nginx(二) 反向代理配置詳解

nginx(二)反向代理配置詳解

nginx反向代理配置好比但願經過瀏覽器輸入http://localhost 直接跳轉到http://www.baidu.com 進入到百度首頁php

在nginx/conf.d/default.conf中配置:html

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
       proxy_pass http://www.baidu.com;//請求轉向自定義的服務器列表 跳轉到百度
       proxy_redirect default;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

下面對nginx.conf文件進行說明:node

  • nginx進程數,建議設置爲等於CPU總核心數nginx

  • 單個進程最大鏈接數,那麼該服務器的最大鏈接數=鏈接數*進程數web

  • 監聽端口通常都爲http端口:80;正則表達式

  • 域名能夠有多個,用空格隔開:例如 server_name www.baidu.com;瀏覽器

負載均衡列表基本配置:服務器

•location / {}:對aspx後綴的進行負載均衡請求,假如咱們要對全部的aspx後綴的文件進行負載均衡時,能夠這樣寫:location ~ .*\.aspx$ {}app

•proxy_pass:請求轉向自定義的服務器列表,這裏咱們將請求都轉向標識爲http://cuitccol.com的負載均衡服務器列表;負載均衡

 

在負載均衡服務器列表的配置中,weight是權重,能夠根據機器配置定義權重(若是某臺服務器的硬件配置十分好,能夠處理更多的請求,那麼能夠 爲其設置一個比較高的weight;而有一臺的服務器的硬件配置比較差,那麼能夠將前一臺的weight配置爲weight=2,後一臺差的配置爲 weight=1)。weigth參數表示權值,權值越高被分配到的概率越大;

 

關於nginx負載均衡策略配置

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections  1024;
}


http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    upstream web_pools{
      server 192.168.102.101:80 weight=1;
      server 192.168.102.102:22222 weight=3;
    }

    include /etc/nginx/conf.d/*.conf;
}

 

/etc/nginx/conf.d/default.d 配置以下:

#
# The default server
#

#upstream baidu{
#   server https://www.baidu.com/;
#}

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  baidu weixin;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
       proxy_pass http://web_pools;
       proxy_redirect default;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

配置參數可參考(https://www.zybuluo.com/phper/note/89391)

常見配置

user root;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
     upstream yytest {
     server 192.168.102.101:80;
}
upstream zztest {
    server 192.168.102.102:80;
}
upstream xxtest {
    server 192.168.102.103:20003;
}
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    server {
        listen       80 ;
        server_name  yytest.witpos.cn;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://yytest;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
                server {
        listen       80 ;
        server_name  zztest.witpos.cn;
        root         /usr/share/nginx/html;
         include /etc/nginx/default.d/*.conf;

        location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://wsctest;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        }
    server {
        listen       80 ;
        server_name  zztest.witpos.cn;
        root         /usr/share/nginx/html;
         include /etc/nginx/default.d/*.conf;

        location / {
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://zztest;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        }

}

 

關於location 正則匹配規則說明

location  = / {
  # 只匹配"/".
  [ configuration A ] 
}
location  / {
  # 匹配任何請求,由於全部請求都是以"/"開始
  # 可是更長字符匹配或者正則表達式匹配會優先匹配
  [ configuration B ] 
}
location ^~ /images/ {
  # 匹配任何以 /images/ 開始的請求,並中止匹配 其它location
  [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg結尾的請求. 
  # 可是全部 /images/ 目錄的請求將由 [Configuration C]處理.   
  [ configuration D ] 
}

請求URI例子:

  • / -> 符合configuration A
  • /documents/document.html -> 符合configuration B
  • /images/1.gif -> 符合configuration C
  • /documents/1.jpg ->符合 configuration D

 

參考文章:

http://www.jianshu.com/p/bed000e1830b

http://www.jianshu.com/p/4b9e00408837

相關文章
相關標籤/搜索