nginx配置(windows配置)


以下是我的項目用到的一份配置文件
#user  nobody;
worker_processes  4; #進程數,一般cpu是幾核就寫多少

#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 {
    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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    proxy_connect_timeout 15s;
    proxy_send_timeout 15s;
    proxy_read_timeout 15s;
    fastcgi_buffers 8 128k;

    gzip  on;
    client_max_body_size 30m;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;
    gzip_vary on;

    #第一個集群
    upstream xdx.com{
       server 119.10.52.28:8081 weight=100;
       server 119.10.52.28:8082 weight=100;
       
    }

    #第二個集群,用於上傳圖片所用
    upstream xdxfile.com{
       server 119.10.52.28:8081;#關於文件上傳的請求均訪問這個集群
    }

   #第三個集群
    upstream xdx8082.com{
       server 119.10.52.28:8082;#8082
    }

   #第四個集群
    upstream xdxali.com{
       server 139.196.235.228:8082;#阿里雲
    }

    #第五個集群
     upstream xdxaliws.com{
       server 139.196.235.228:8886;#阿里雲websocket
    }
#第一個代理服務器,監聽的是80端口,監聽的域名是www.wonyen.com或者wonyen.com
    server {
        listen       80;#監聽的端口
        server_name  www.wonyen.com wonyen.com;#監聽的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


       #location指的是訪問的路徑,下面這條配置表示當訪問網站的根目錄,即訪問wonyen.com或者www.wonyen.com的時候,就去根目錄為html的下面去尋找index.html或者index.htm。在index.html這個頁面里面你可以做一些重定向的工作,跳轉到指定頁面

       #也可以自定義到某個集群
       # location / {
           # root   html;
           # index  index.html index.htm;
        #}
        #所有靜態請求都交由nginx處理,存放目錄為webapps下的root,過期時間為30天
         location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {  
            root    apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;  
            expires 30d;
        }

        #配置以Att結尾的請求的處理集群為http://xdxfile.com
        location ~ ^/\w+Att{
            proxy_pass http://xdxfile.com;
        }

        #配置以Fill結尾的請求的處理集群為http://xdxfile.com
        location ~ ^/\w+Fill{
            proxy_pass http://xdxfile.com;
        }

        #精准配置,如果請求名為/crowdFundSave,則
        location = /crowdFundSave{
            proxy_pass http://xdxfile.com;
        }

      #精確配置,同上
        location = /crowdFundRewardSave{
            proxy_pass http://xdxfile.com;
        }

     #精確配置,同上
        location = /garbageCategorySave{
            proxy_pass http://xdxfile.com;
        }

      #精確配置,同上
        location = /mailTestAjax{
            proxy_pass http://xdx8082.com;
        }

       #精確配置,同上
        location = /mailSendAjax{
            proxy_pass http://xdx8082.com;
        }

      #精確配置,同上
        location = /mailOldAjax{
            proxy_pass http://xdx8082.com;
        }

       #精確配置,同上
        #location = /wechatAuthority{
            #proxy_pass http://xdxali.com;
        #}
        location ~ ^/ueditor1_4_3{
            proxy_pass http://xdxfile.com;
        }
        #其他所有請求都訪問  http://xdx.com的集群    
         location ~ .*$ {
            index index;
            proxy_pass http://xdx.com;
         }
       #404頁面訪問/Error404.jsp這個location
        error_page  404              /Error404.jsp;

        #500等頁面也訪問  /Error404.jsp這個location
        error_page   500 502 503 504  /Error404.jsp;

        #配置請求/Error404.jsp就訪問http://xdxfile.com集群
        location = /Error404.jsp {
         proxy_pass http://xdxfile.com;
        }

        # 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
    #另外一個代理服務器,監聽8886接口,監聽的域名為www.wonyen.com或者wonyen.com
    server {
        listen       8886;
        server_name  www.wonyen.com wonyen.com;
#配置若請求為wonyen.com:8086(根目錄),就讓他去訪問http://xdxaliws.com這個集群,這邊配置的是websocket的服務端
     location / {
            proxy_pass http://xdxaliws.com;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }


    # 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.集群的配置,我在上面的配置里有定義多個集群,集群按字面的意思理解就是由多台服務器構成的一個集合,典型的例子如  

  upstream xdx.com{
       server 119.10.52.28:8081 weight=100;
       server 119.10.52.28:8082 weight=100;
       
    }

這樣的一個配置,這個集群包含了兩個分支,我們可以在兩台服務器上搭建相同的項目(上述的例子是在同樣的服務器,不同的端口部署相同的項目,因為筆者的服務器有限),當有請求是需要這個集群來處理的時候,nginx會隨機分配,當然也可以配置權重來設置兩個server的訪問概率。這就是負載均衡的原理。我們在多台服務器上部署相同的項目,利用nginx對請求進行轉發,這樣可以降低只有一台服務器所造成的的負載過大,而且當其中一台服務器掛掉以后,nginx會分配另外一台服務器來工作,這樣就不會造成服務停止了。

2.server配置項代表的是一個代理服務器,上面的文件中我們配置了兩個文件,分別監聽wonyen.com(www.wonyen.com)這兩個域名的80和8886端口,所有訪問wonyen.com:80(即wonyen.com)這個域名下的請求,都按照第一個server所定義的規則去轉發,而所有訪問wonyen.com:8886下的請求,則會按照第二個server所定義的規則去轉發。

3.我們甚至可以通過配置來處理多個域名,看以下的例子。下面的例子我配置了兩個域名的規則,一個是iis服務器,一個是tomcat服務器,主要目的是為了解決80端口只能被一個程序使用的問題。如果iis用了80,tomcat就用不了,反之亦然。所以我給iis和tomcat都分配除了80以外的端口,而把80端口留給niginx。由nginx來分配請求給不同的網站。

#user  nobody;
worker_processes  1;

#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 {
    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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    client_max_body_size 30m;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;
    gzip_vary on;
    upstream achina.com{
       server 120.76.129.218:81;
       
    }
    upstream qgrani.com{
       server 120.76.129.218:8080;
       
    }

    server {
        listen       80;
        server_name  www.achinastone.com achinastone.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
          #其他請求        
         location ~ .*$ {
            index index;
            proxy_pass http://achina.com;
         }

        #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       80;
        server_name  www.qgranite.com qgranite.com;

       location / {
           root   html;
            index  index.html index.htm;
        }
         #所有靜態請求都交由nginx處理,存放目錄為webapp
         location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v)$ {  
            root    apache-tomcat-8.0.9\webapps\ROOT;  
            expires 30d; 
        }
           #其他請求        
         location ~ .*$ {
            index index;
            proxy_pass http://qgrani.com;
         }
    }


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

}

 4.還有一個就是動靜分離,說得通俗一點就是,把請求數據(動)與請求圖片(靜)分開,在tomcat里,當我們沒有做動靜分離的時候,tomcat把對圖片的請求也會當成一個動態的請求,而處理動態請求是比較費性能的(至於為什么,我也不太清楚)。所以我們可以使用nginx配置來實現動靜分離。

我的做法是把其中一個tomcat項目放在nginx的根目錄下,這樣,我們就可以通過以下方式來配置,實現當我們訪問圖片,js,css等靜態資源的時候,都到一個指定的目錄去訪問。這樣做的好處除了節省性能,還有一個就是我們不需要在所有的負載均衡服務器中都同步保留這些靜態資源,只需要在一個地方保留就好了。配置如下

        #所有靜態請求都交由nginx處理,存放目錄為webapps下的root,過期時間為30天
         location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {  
            root    apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;  
            expires 30d;
        }

5.既然讀取靜態資源是從這個目錄讀取的,那么我們必須考慮如何存儲靜態資源,特別是當我們做了負載均衡以后,在我們的 項目中上傳圖片的請求有可能在任意一個集群的分支中被調用,比如我們的集群中有A,B兩台服務器,他們都有可能做上傳圖片這件事情,如果A調用了上傳圖片這個請求,則圖片則被上傳到了A這台服務器上,反之就是B上面。這樣勢必導致A,B兩台服務器上的靜態圖片是不同步的,當我們要訪問這些圖片的時候,(假設此時我們還沒做動靜分離)就有可能出現訪問不到的情況。由於上一步我們做了動靜分離,現在的問題就演變為,如何把這些A,B服務器上傳的圖片,都同步到我們做動靜分離的那個文件夾下。人工或者程序去同步都很麻煩,我的做法是指定一台服務器(也就是nginx安裝的那台服務器)的tomcat項目(也就是部署在nginx根目錄下的那個tomcat項目),讓它專門來負責上傳圖片的工作,這樣所有的圖片都由這個tomcat項目來上傳,也就保證了靜態庫中的圖片就是完整的圖片。為此我配置了一個集群,如下。

    #第二個集群,用於上傳圖片所用
    upstream xdxfile.com{
       server 119.10.52.28:8081;#關於文件上傳的請求均訪問這個集群
    }

然后在location中我這樣配置:

        #配置以Att結尾的請求的處理集群為http://xdxfile.com
        location ~ ^/\w+Att{
            proxy_pass http://xdxfile.com;
        }

        #配置以Fill結尾的請求的處理集群為http://xdxfile.com
        location ~ ^/\w+Fill{
            proxy_pass http://xdxfile.com;
        }

因為我把所有涉及到附件上傳的請求都加上了Att或者Fill的后綴,當nginx捕獲這些后綴名的請求的時候,就會把他們都交給 http://xdxfile.com這個集群,也就是119.10.52.28:8081這個項目。

6.做了負載均衡以后,有一個不得不面臨的問題就是內存數據的同步,我們在程序中有時候會把一些數據存放在內存中,典型的一類數據就是session。如何讓session數據在集群的各個分支中共享session呢,這邊要用到一個新的東西,叫做redis。我會在下一篇文章中詳細地介紹。

7.有時候,我們會使用諸如request.getServerName(),request.getServerPort()這樣的servlet的方法來獲取當前項目的域名和端口,由於做了反向代理,往往我們獲取到的域名和端口不是實際的域名和端口,而是反向代理的域名和端口。舉個例子,我們給域名

oa.wonyen.com做了一個集群。

upstream oa.com{
       server 139.196.235.228:8086;
    }

我們配置一個location,如下所示。

server {
        listen       80;
        server_name  oa.wonyen.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
            #root   html;
            #index  index.html index.htm;
        #}
          #其他請求        
         location ~ .*$ {
            index index;
            proxy_pass http://oa.com;
         }
}

如果我們此時在訪問oa.wonyen.com,它會反向代理給http://oa.com;這個集群,也就是139.196.235.228:8086這個端口所對應的項目。此時我們通過request.getServerName()獲取的是代理的名稱,oa.com而不是真正的域名oa.wonyen.com。這就會造成一些錯誤,所以我們需要添加一條配置來避免這個錯誤。在location中多加一條語句,如下所示。

    location ~ .*$ {
            index index;
            proxy_pass http://oa.com;
          proxy_set_header Host $host;
         }

它告訴程序要訪問的host是實際的域名而不是代理的域名。


免責聲明!

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



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