微服務深入淺出(9)-- Nginx


Nginx ("engine x") 是一個高性能的HTTP反向代理服務器,處理請求是異步非阻塞的,多個連接(萬級別)可以對應一個進程。而Apache是同步多進程模型,一個連接對應一個進程。

#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壓縮
    #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.121  weight=1;
      server 192.168.1.122  weight=2;
    }

    #配置代理服務器的地址,即Nginx安裝的服務器地址、監聽端口、默認地址
    server {
        #1.偵聽80端口 
        listen       80;

        #對於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對應   
        }

        #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;
    #    }
    #}

}

注意:

1、文件目錄不能出現中文

2、 tasklist /fi "imagename eq nginx.exe" 查看是否啟動成功

3、Nginx優化

1.    gzip壓縮優化
2.    expires緩存有還
3.    網絡IO事件模型優化
4.    隱藏軟件名稱和版本號
5.    防盜鏈優化
6.    禁止惡意域名解析
7.    禁止通過IP地址訪問網站
8.    HTTP請求方法優化
9.    防DOS攻擊單IP並發連接的控制,與連接速率控制
10.    嚴格設置web站點目錄的權限
11.    將nginx進程以及站點運行於監牢模式
12.    通過robot協議以及HTTP_USER_AGENT防爬蟲優化
13.    配置錯誤頁面根據錯誤碼指定網頁反饋給用戶
14.    nginx日志相關優化訪問日志切割輪詢,不記錄指定元素日志、最小化日志目錄權限
15.    限制上傳到資源目錄的程序被訪問,防止木馬入侵系統破壞文件
16.    FastCGI參數buffer和cache配置文件的優化
17.    php.ini和php-fpm.conf配置文件的優化
18.    有關web服務的Linux內核方面深度優化(網絡連接、IO、內存等)
19.    nginx加密傳輸優化(SSL)
20.    web服務器磁盤掛載及網絡文件系統的優化
21.    使用nginx cache

4、Nginx+Keepalive負載均衡


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM