1. 程式人生 > 實用技巧 >java基礎之二:取整函式(Math類)

java基礎之二:取整函式(Math類)

Nginx 配置反向代理

一、前言

反向代理作用

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

舉例

比如小編的碼雲個人部落格地址為:http://zhengqingya.gitee.io/blog/,現在小編想通過自己的伺服器地址http://www.zhengqing520.com/blog/來訪問到碼雲上面個人部落格的地址,並且訪問地址是自己的伺服器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 轉發到http://zhengqingya.gitee.io

server {
    listen       80;
    server_name  www.zhengqing520.com;# 伺服器地址或繫結域名

    location / { # 訪問80埠後的所有路徑都轉發到 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 轉發到http://www.zhengqing520.com:9528/api/
  2. www.zhengqing520.com/blog/ 轉發到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作用:設定傳送到後端伺服器(上面proxy_pass)的請求頭值  
            # 【當Host設定為 $http_host 時,則不改變請求頭的值;
            #   當Host設定為 $proxy_host 時,則會重新設定請求頭中的Host資訊;
            #   當為$host變數時,它的值在請求包含Host請求頭時為Host欄位的值,在請求未攜帶Host請求頭時為虛擬主機的主域名;
            #   當為$host:$proxy_port時,即攜帶埠傳送 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; # 改變請求頭值 -> 轉發到碼雲才會成功 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作用:設定傳送到後端伺服器(上面proxy_pass)的請求頭值  
                # 【當Host設定為 $http_host 時,則不改變請求頭的值;
                #   當Host設定為 $proxy_host 時,則會重新設定請求頭中的Host資訊;
                #   當為$host變數時,它的值在請求包含Host請求頭時為Host欄位的值,在請求未攜帶Host請求頭時為虛擬主機的主域名;
                #   當為$host:$proxy_port時,即攜帶埠傳送 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; # 改變請求頭值 -> 轉發到碼雲才會成功
            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;
        }

   }
}