WINDOWS 2008Server 配置nginx 反向 Proxy伺服器

來源:互聯網
上載者:User

標籤:反向 Proxy   nginx   nginx負載平衡   windows反向 Proxy伺服器   

本案例有用過可行

0、先要在網域名稱官網上面佈建網域名對應的IP地址,然後要在自己路由器上面將80連接埠映射到要裝nginx伺服器的IP地址。

1、從官網上面下載nginx1.6.2   WINDOWS版本的。訪問地址http://nginx.org/en/download.html

2、解壓縮到C盤根目錄下面

3、複製C:\nginx\conf\nginx.conf,儲存成一個副本

4、編輯nginx.conf,內容如下

#背景工作處理序數,建議設定為CPU的總核心數

worker_processes  2;

#全域錯誤記錄檔定義類型,日誌等級從低到高依次為:

#debug | info | notice | warn | error | crit

error_log  logs/error.log  info;

#記錄主進程ID的檔案

pid        /nginx/nginx.pid;


#一個進程能開啟的檔案描述符最大值,理論上該值因該是最多能開啟的檔案數除以進程數。

#但是由於nginx負載並不是完全均衡的,所以這個值最好等於最多能開啟的檔案數。

#LINUX系統可以執行 sysctl -a | grep fs.file 可以看到linux檔案描述符。

worker_rlimit_nofile 65535;


#串連數上限, 單個進程允許的最大串連數

events {   

    worker_connections  65535;

}


#設定http伺服器,利用它的反向 Proxy功能提供負載平衡支援

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 記錄了哪些使用者,哪些頁面以及使用者瀏覽器、ip和其他的訪問資訊

    access_log  logs/access.log  main;


    #伺服器名字的hash表大小

    server_names_hash_bucket_size 128;


    #用戶端要求標頭緩衝大小。

    #nginx預設會用client_header_buffer_size這個buffer來讀取header值,

    #如果header過大,它會使用large_client_header_buffers來讀取。

    #如果設定過小HTTP頭/Cookie過大 會報400 錯誤 nginx 400 bad request

    #如果超過buffer,就會報HTTP 414錯誤(URI Too Long)

    #nginx接受最長的HTTP頭部大小必須比其中一個buffer大

    #否則就會報400的HTTP錯誤(Bad Request)

    client_header_buffer_size 32k;

    large_client_header_buffers 4 32k;

    #用戶端請求體的大小

    client_body_buffer_size    8m;

    #隱藏ngnix版本號碼

    server_tokens off;

    #忽略不合法的要求標頭

    ignore_invalid_headers   on;

    #指定啟用除第一條error_page指令以外其他的error_page。

    recursive_error_pages    on;

    #讓 nginx 在處理自己內部重新導向時不預設使用  server_name 設定中的第一個網域名稱

    server_name_in_redirect off;

    #開啟檔案傳輸,一般應用都應設定為on;若是有下載的應用,則可以設定成off來平衡網路I/O和磁碟的I/O來降低系統負載

    sendfile                 on;

    #告訴nginx在一個資料包裡發送所有標頭檔,而不一個接一個的發送。

    tcp_nopush     on;

    #告訴nginx不要快取資料,而是一段一段的發送--當需要及時發送資料時,就應該給應用設定這個屬性,

    #這樣發送一小塊資料資訊時就不能立即得到傳回值。

    tcp_nodelay    on;

    #長連線逾時時間,單位是秒

    keepalive_timeout  65;

    #gzip模組設定,使用 gzip 壓縮可以降低網站頻寬消耗,同時提升訪問速度。

    gzip  on;             #開啟gzip

    gzip_min_length  1k;          #最小壓縮大小

    gzip_buffers     4 16k;        #壓縮緩衝區

    gzip_http_version 1.0;       #壓縮版本

    gzip_comp_level 2;            #壓縮等級

    gzip_types       text/plain application/x-javascript text/css application/xml;           #壓縮類型

    #upstream作負載平衡,在此配置需要輪詢的伺服器位址和連接埠號碼,max_fails為允許請求失敗的次數,預設為1.


    #weight為輪詢權重,根據不同的權重分配可以用來平衡伺服器的訪問率。


    #指定要網域名稱對應的WEB項目訪問地址

    upstream hostname {

        server 192.168.33.129:18080 max_fails=0 weight=1;

    }


    #主機配置

    server {

        #監聽連接埠

        listen       80;

        #網域名稱

        server_name  hostname;

        #字元集

        charset utf-8;

        #單獨的access_log檔案

        access_log  logs/192.168.33.129.access.log  main;

        #反向 Proxy配置,

        #將所有請求為http://hostname的請求全部轉寄到upstream中定義的目標伺服器中。

        location / {

            #此處配置的網域名稱必須與upstream的網域名稱一致,才能轉寄。

            proxy_pass     http://hostname;

            proxy_set_header   X-Real-IP $remote_addr;

        }


        #啟用nginx status 監聽頁面


        location /nginxstatus {

            stub_status on;

            access_log on;

        }

        #錯誤頁面

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }


    }

upstream hostname1 {

        server 192.168.33.129:28080 max_fails=0 weight=1;

}


       #主機配置

    server {

        #監聽連接埠

        listen       80;

        #網域名稱

        server_name  hostname1;

        #字元集

        charset utf-8;

        #單獨的access_log檔案

        access_log  logs/192.168.33.129.access.log  main;

        #反向 Proxy配置,

        #將所有請求為http://hostname1的請求全部轉寄到upstream中定義的目標伺服器中。

        location / {

            #此處配置的網域名稱必須與upstream的網域名稱一致,才能轉寄。

            proxy_pass     http://hostname1;

            proxy_set_header   X-Real-IP $remote_addr;

        }


        #啟用nginx status 監聽頁面


        location /nginxstatus {

            stub_status on;

            access_log on;

        }

        #錯誤頁面

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }


    }


}


到此配置完成

5、啟動nginx

開始菜單-->運行-->cmd-->

cd c:\nginx\

nginx

啟動完成

6、訪問

現在可以在瀏覽器中輸入u-pai,youjie.co訪問到不同的網站。

文章參考:http://blog.csdn.net/zxcvqwer19900720/article/details/24991427

本文出自 “7422562” 部落格,請務必保留此出處http://7432562.blog.51cto.com/7422562/1575586

WINDOWS 2008Server 配置nginx 反向 Proxy伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.