隱藏服務器信息 -> 保證內網的安全,一般將反向代理做爲公網訪問地址,web服務器是內網,即經過nginx配置外網訪問web服務器內網html
好比小編的碼雲我的博客地址爲:http://zhengqingya.gitee.io/b... ,如今小編想經過本身的服務器地址 http://www.zhengqing520.com/b... 來訪問到碼雲上面我的博客的地址,而且訪問地址是本身的服務器ip或者域名地址,這時候咱們就能夠經過Nginx配置反向代理來實現 ~nginx
咱們能夠經過 proxy_pass 來配置git
小編是經過docker拉取的nginx,默認配置文件是nginx.conf中引入包含的default.conf文件
也就是說nginx.conf配置文件中有以下一個配置web
include /etc/nginx/conf.d/*.conf;
注:這裏小編將個人default.conf配置文件中的內容提到nginx.conf配置文件中來實現
即註釋 include /etc/nginx/conf.d/*.conf;
好比 www.zhengqing520.com 轉發到 http://zhengqingya.gitee.iodocker
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://】 } }
根據不一樣的後綴名訪問不一樣的服務器地址後端
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配置文件中的所有內容以供參考api
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; } } }