Nginx配置反向代理

一、前言

反向代理作用

隱藏服務器信息 -> 保證內網的安全,通常將反向代理作為公網訪問地址,web服務器是內網,即通過nginx配置外網訪問web服務器內網

創(chuàng)新互聯-專業(yè)網站定制、快速模板網站建設、高性價比蔡甸網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式蔡甸網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋蔡甸地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。

舉例

比如小編的碼云個人博客地址為:http://zhengqingya.gitee.io/blog/ ,現在小編想通過自己的服務器地址 http://www.zhengqing520.com/blog/ 來訪問到碼云上面?zhèn)€人博客的地址,并且訪問地址是自己的服務器ip或者域名地址,這時候我們就可以通過Nginx配置反向代理來實現 ~

二、Nginx如何配置反向代理呢?

我們可以通過 proxy_pass來配置

(1)找到nginx配置文件 nginx.conf
溫馨小提示

小編是通過docker拉取的nginx,默認配置文件是nginx.conf中引入包含的default.conf文件
也就是說nginx.conf配置文件中有如下一個配置

include /etc/nginx/conf.d/*.conf;
(2)修改配置 -> 實現反向代理

注:這里小編將我的default.conf配置文件中的內容提到nginx.conf配置文件中來實現
即注釋 include /etc/nginx/conf.d/*.conf;

簡單配置

比如 www.zhengqing520.com 轉發(fā)到 http://zhengqingya.gitee.io

server {
    listen       80;
    server_name  www.zhengqing520.com;# 服務器地址或綁定域名

    location / { # 訪問80端口后的所有路徑都轉發(fā)到 proxy_pass 配置的ip中
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://zhengqingya.gitee.io; # 配置反向代理的ip地址和端口號 【注:url地址需加上http:// 或 https://】
    }
}
復雜配置

根據不同的后綴名訪問不同的服務器地址

  1. www.zhengqing520.com/api 轉發(fā)到 http://www.zhengqing520.com:9528/api/
  2. www.zhengqing520.com/blog/ 轉發(fā)到 http://zhengqingya.gitee.io/blog/
server {
    listen       80;
    server_name  www.zhengqing520.com;# 服務器地址或綁定域名

    location ^~ /api {  # ^~/api 表示匹配前綴為api的請求
        proxy_pass  http://www.zhengqing520.com:9528/api/;  # 注:proxy_pass的結尾有/, -> 效果:會在請求時將/api/*后面的路徑直接拼接到后面

        # proxy_set_header作用:設置發(fā)送到后端服務器(上面proxy_pass)的請求頭值  
            # 【當Host設置為 $http_host 時,則不改變請求頭的值;
            #   當Host設置為 $proxy_host 時,則會重新設置請求頭中的Host信息;
            #   當為$host變量時,它的值在請求包含Host請求頭時為Host字段的值,在請求未攜帶Host請求頭時為虛擬主機的主域名;
            #   當為$host:$proxy_port時,即攜帶端口發(fā)送 ex: $host:8080 】
        proxy_set_header Host $host; 

        proxy_set_header X-Real-IP $remote_addr; # 在web服務器端獲得用戶的真實ip 需配置條件①    【 $remote_addr值 = 用戶ip 】
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 在web服務器端獲得用戶的真實ip 需配置條件②
        proxy_set_header REMOTE-HOST $remote_addr;
        # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for變量 = X-Forwarded-For變量
    }

    location ^~ /blog/ { # ^~/blog/ 表示匹配前綴為blog/后的請求
        proxy_pass  http://zhengqingya.gitee.io/blog/; 

        proxy_set_header Host $proxy_host; # 改變請求頭值 -> 轉發(fā)到碼云才會成功
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
    }
}

三、總結

這里再給出一下小編nginx配置文件中的全部內容以供參考

user  nginx;
worker_processes  1;

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

events {
    worker_connections  1024;
}

http {
    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;

    # include /etc/nginx/conf.d/*.conf; # 引入default.conf配置文件

    server {
        listen       80;
        server_name  www.zhengqing520.com;# 服務器地址或綁定域名

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        # start ---------------------------------------------------------------------------------------------

        location / {
            root   /usr/share/nginx/html;
            try_files $uri $uri/ @router;
            index  index.html index.htm;
            # proxy_pass http://zhengqingya.gitee.io; # 代理的ip地址和端口號
            # proxy_connect_timeout 600; #代理的連接超時時間(單位:毫秒)
            # proxy_read_timeout 600; #代理的讀取資源超時時間(單位:毫秒)
        } 

        location @router {
            rewrite ^.*$ /index.html last;  
        }

        location ^~ /api {  # ^~/api/表示匹配前綴為api的請求
            proxy_pass  http://www.zhengqing520.com:9528/api/;  # 注:proxy_pass的結尾有/, -> 效果:會在請求時將/api/*后面的路徑直接拼接到后面

            # proxy_set_header作用:設置發(fā)送到后端服務器(上面proxy_pass)的請求頭值  
                # 【當Host設置為 $http_host 時,則不改變請求頭的值;
                #   當Host設置為 $proxy_host 時,則會重新設置請求頭中的Host信息;
                #   當為$host變量時,它的值在請求包含Host請求頭時為Host字段的值,在請求未攜帶Host請求頭時為虛擬主機的主域名;
                #   當為$host:$proxy_port時,即攜帶端口發(fā)送 ex: $host:8080 】
            proxy_set_header Host $host; 

            proxy_set_header X-Real-IP $remote_addr; # 在web服務器端獲得用戶的真實ip 需配置條件①    【 $remote_addr值 = 用戶ip 】
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 在web服務器端獲得用戶的真實ip 需配置條件②
            proxy_set_header REMOTE-HOST $remote_addr;
            # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for變量 = X-Forwarded-For變量
        }

        location ^~ /blog/ { # ^~/blog/ 表示匹配前綴為blog/后的請求
            proxy_pass  http://zhengqingya.gitee.io/blog/;   

            proxy_set_header Host $proxy_host; # 改變請求頭值 -> 轉發(fā)到碼云才會成功
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
        }

        # end ---------------------------------------------------------------------------------------------

        #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   /usr/share/nginx/html;
        }

   }
}

分享標題:Nginx配置反向代理
當前網址:http://muchs.cn/article12/iidegc.html

成都網站建設公司_創(chuàng)新互聯,為您提供企業(yè)建站、網站制作App開發(fā)、網站排名、微信小程序、移動網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

網站建設網站維護公司