Nginxチュートリアル

7903 ワード

一、Nginxのダウンロードとインストール
1、nginx公式ダウンロードアドレス:http://nginx.org/
2、ダウンロードが終わったら圧縮パックを解凍すればいい
3、nginxプロファイルはルートディレクトリの下confginxである.conf

Nginx教程_第1张图片
二、Nginx常用コマンド説明
cmd nginxインストールディレクトリへ
tasklist /fi "imagename eq nginx.exe"      nginx  
start nginx          nginx
nginx -s quit          
nginx -s stop          
nginx -s reload           ,  nginx    ,     
nginx -s reopen          
nginx -v               
nginx -h                 

拡張:問題の開始
一、80ポートが占有されています.ここではconfのnginxに行くことができます.confでnginxポートを変更します(私はこの方法を使用しています)

二、80ポートの占用プログラムを閉じる(ネット上の方法)
出典:https://www.jianshu.com/p/7242b0ba324e
最終アクセス:http://localhost:8900/ 
Nginx教程_第2张图片
Nginx教程_第3张图片
以上の2つの方法はいずれもnginxの起動に成功したフラグである.
三、Nginxプロファイルの詳細
#user  nobody;
 
#   nginx   
worker_processes 1;
 
#        PID  
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
events {
    #      
    worker_connections  1024;
}
 
#  http   ,                  
http {
    
    #  mime  ,   mime.type    
    include       mime.types;
    default_type  application/octet-stream;
 
    #      
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
    #          
    #access_log  logs/access.log  main;
 
    # sendfile      nginx    sendfile   (zero copy   )     ,      
    sendfile        on;
    #tcp_nopush     on;
    
    #       
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #  gzip  ,  html
    #gzip  on;
###################################
    #                       ,      upstream        Server.
    # nginx   upstream          
    # 1.  (  )                      ,       down ,     。
    # 2.weight       ,weight        ,              。
    # 3.ip_hash        ip hash    ,                 ,    session   
    # 4.fair
    # 5.url_hash #Urlhash
    upstream mysvr{
        #weigth      ,             
        #1.down      server       
        #2.weight    1 weight  ,        
        #3.backup       backup  down      ,  backup  ,           ,    
        
        #server 192.168.1.116    down;
        #server 192.168.1.116    backup;
        #server 192.168.1.142    weight=1;
        server 192.168.1.142    weight=1;
    }
#####################################
 
    #           , Nginx        、    、    
    server {
        #1.  8900  
        listen       8900;
        #  server_name,                  ,      server_name     
        server_name  localhost;
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            #        nginx     html   
            root   html ;
            index  index.html index.htm;
            #proxy_pass http://mysvr; #         upstream  
        }
        #    E:/source         localhost:8099/file/a.png         E:/source/file/a.png
        location /file/ {
            root E:/source/;
            autoindex on;
        }
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #         
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
 
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

四、nginxエージェントサーバーを使用して負荷均衡を行う
nginxのプロファイルnginxを変更できます.confはnginxエージェントサーバへのアクセス時に指定したサーバにジャンプする目的、すなわちproxy_passはリクエスト転送アドレスを構成します.すなわち、私たちがまだ入力している場合http://localhost:80リクエストは、構成されているサーバにジャンプします.
Nginx教程_第4张图片
同様に、複数のターゲット・サーバを構成できます.nginxは、1つのサーバに障害が発生した場合、要求を自動的に別のサーバに移行できます.たとえば、次のように構成されています.
Nginx教程_第5张图片
サーバlocalhost:8080が停止すると、nginxは要求をサーバ192.168.101.9:8080に自動的に移行することができる.各サーバがアクセスする重みを示すweightプロパティも追加されており、weightが高いほどアクセスされる確率が高くなります.
五.nginx静的リソースの構成
jpg|png|css|jsなどの静的リソースを、次の構成のf:/nginx-1.12.2/staticディレクトリの下に配置し、nginxプロファイルで次の構成を行います(静的リソース構成はlocation/にのみ使用できます)、ブラウザでアクセスします.http://localhost:80/1.pngf:/nginx-1.12.2/staticディレクトリの1にアクセスできます.pngピクチャ
Nginx教程_第6张图片