Nginx反向 Proxy設定總結

來源:互聯網
上載者:User
負載平衡技術有很多種,常用的四/七層負載平衡技術包含很多,Nginx反向 Proxy就是其中的一種方案。

以下的配置就是Nginx反向 Proxy配置範例:

user  www www;worker_processes  10;error_log  /data1/logs/error.log crit;pid        /usr/local/webserver/nginx/nginx.pid;worker_rlimit_nofile 51200;events {use epoll;    worker_connections  51200;}http {    include       mime.types;    default_type  application/octet-stream;#charset utf-8server_names_hash_buckets_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;    #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;tcp_nodelay on;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;    gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.1;gzip_comp_level 2;gzip_types text/plain application/javascript text/css application/xml;gzip_vary on;client_max_body_size 300m;client_body_buffer_size 128k;proxy_connect_timeout 600;proxy_read_timeout 600;proxy_send_timeout 600;proxy_buffer_size 16k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;upstream php_server_pool {  server 192.168.1.10:80 weight=4 max_fails=2 fail_timeout=30s;  server 192.168.1.11:80 weight=4 max_fails=2 fail_timeout=30s;  server 192.168.1.12:80 weight=4 max_fails=2 fail_timeout=30s;}upstream message_server_pool {  server 192.168.1.13:3245;  server 192.168.1.14:3245 down;}upstream php_server_pool {  server 192.168.1.15:80 weight=1 max_fails=2 fail_timeout=30s;  server 192.168.1.16:80 weight=1 max_fails=2 fail_timeout=30s;  server 192.168.1.17:80 weight=1 max_fails=2 fail_timeout=30s;  server 192.168.1.18:80 weight=1 max_fails=2 fail_timeout=30s;}    server {        listen       80;        server_name  www.yourdomain.com;        #charset koi8-r;        access_log  /data1/logs/www.yourdomain.com_access.log;location / {proxy_next_upstream http_502 http_504 error timeout invalid_header;proxy_pass http://php_server_pool;proxy_set_header Host www.yourdomain.com;proxy_set_header X-Forwarded-For $remote_addr;        }        # 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;        #}    }server {listen 80;server_name www1.yourdomain.com;localtion /message/ {proxy_pass http://message_server_pool;proxy_set_header Host $host;}localtion / {proxy_pass http://php_server_pool;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;}access_log  /data1/logs/message.yourdomain.com_access.log;}server {listen 80;server_name bbs.yourdomain.com *.bbs.yourdomain.com;location / {proxy_pass http://bbs_server_pool;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;}access_log off;}    # 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;    #    }    #}}
以上的配置中,upstream這個是用來配置一組可以在proxy_pass或者fastcgi_pass中使用的一組Proxy 伺服器,預設的負載平衡方式是輪詢。server用來指定伺服器的名稱和參數,名稱可以是一個網域名稱、一個IP地址、連接埠號碼或者UNIX Socket。

proxy_pass和fastcgi_pass可以設定反向 Proxy的upstream叢集。

proxy_set_header用於向反向 Proxy的伺服器發送請求時增加指定的Header資訊。使用Host參數是為了讓後端伺服器知道由哪個虛擬機器主機來處理,使用X-Forwarded-For是為了讓後端伺服器可以通過$_SERVER["HTTP_X_FORWARDED_FOR"]擷取到使用者的真實IP。

以上就介紹了Nginx反向 Proxy設定總結,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.