Nginx代理服務基本概述
代理:
代理一詞往往並不陌生, 該服務我們常常用到如(代理理財、代理租房、代理收貨等等),如下圖所示
沒有代理情景
在沒有代理模式的情況下,客戶端和Nginx服務端,都是客戶端直接請求服務端,服務端直接響應客戶端。
企業場景
那麼在互聯網請求裏面,客戶端往往無法直接向服務端發起請求,那麼就需要用到代理服務,來實現客戶端和服務通信,如下圖所示*
Nginx代理服務常見模式
Nginx作爲代理服務,按照應用場景模式進行總結,代理分爲正向代理、反向代理
正向代理
正向代理,(內部上網)客戶端<—>代理->服務端*
反向代理
反向代理,用於公司集羣架構中,客戶端->代理<—>服務端*
正向代理與反向代理的區別
1.區別在於形式上服務的”對象”不一樣*
2.正向代理代理的對象是客戶端,爲客戶端服務
3.反向代理代理的對象是服務端,爲服務端服務
Nginx代理服務支持協議
Nginx作爲代理服務,可支持的代理協議非常的多,具體如下圖
反向代理使用協議
如果將Nginx作爲反向代理服務,常常會用到如下幾種代理協議,如下圖所示*

模塊總結
反向代理模式與Nginx代理模塊總結如表格所示*
反向代理模式 | Nginx配置模塊 |
---|---|
http、websocket、https | ngx_http_proxy_module |
fastcgi | ngx_http_fastcgi_module |
uwsgi | ngx_http_uwsgi_module |
grpc | ngx_http_v2_module |
Nginx反向代理配置語法
代理配置語法
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
http://localhost:8000/uri/
http://192.168.56.11:8000/uri/
http://unix:/tmp/backend.socket:/uri/
url跳轉修改返回location
url
跳轉修改返回Location
[不常用]
參考下載站點:http://test.driverzeng.com/Nginx_File/
Syntax: proxy_redirect default;
proxy_redirect off;proxy_redirect redirect replacement;
Default: proxy_redirect default;
Context: http, server, location
添加發往後端服務器的請求頭信息
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location
# 用戶請求的時候HOST的值是www.oldboy.com, 那麼代理服務會像後端傳遞請求的還是www.oldboy.com
proxy_set_header Host $http_host;
# 將$remote_addr的值放進變量X-Real-IP中,$remote_addr的值爲客戶端的ip
proxy_set_header X-Real-IP $remote_addr;
# 客戶端通過代理服務訪問後端服務, 後端服務通過該變量會記錄真實客戶端地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
代理到後端的TCP連接、響應、返回等超時時間
//nginx代理與後端服務器連接超時時間(代理連接超時)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
//nginx代理等待後端服務器的響應時間
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
//後端服務器數據回傳給nginx代理超時時間
Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
proxy_buffer代理緩衝區
//nignx會把後端返回的內容先放到緩衝區當中,然後再返回給客戶端,邊收邊傳, 不是全部接收完再傳給客戶端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
//設置nginx代理保存用戶頭信息的緩衝區大小
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
//proxy_buffers 緩衝區
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location
常用優化配置
Proxy代理網站常用優化配置如下,將配置寫入新文件,調用時使用include引用即可
[root@Nginx ~]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
重複使用配置
代理配置location時調用方便後續多個Location重複使用
代理配置location 時調用方便後速多個location重複使用
location / {
proxy_pass http://127.0.0.1:8080;
include proxy_params;
}
Nginx反向代理場景實踐
nginx反向代理配置實例
環境準備
角色 | 外網IP(NAT) | 內網IP(LAN) | 主機名 |
---|---|---|---|
Proxy | eth0:10.0.0.5 | eth1:172.16.1.5 | lb01 |
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | web01 |
配置需求
web01服務器,配置一個網站,監聽在8080此時網站僅172網段的用戶能訪問
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim web.conf
server {
listen 8080;
server_name localhost;
location / {
root /code_8080;
index index.html;kk
deny 10.0.0.0/24;
allow all;
}
}
[root@web01 conf.d]# mkdir /code_8080
[root@web01 conf.d]# echo "web01-7...." >/code_8080/index.html
[root@web01 conf.d]# systemctl restart nginx
配置需求
proxy代理服務,配置監聽eth0的80端口,使10.0.0.0網段的用戶,能夠通過代理服務器訪問到後端的172.16.1.7的8080端口站點內容
[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# vim proxy_web_node1.conf
server {
listen 80;
server_name nginx.oldboy.com;
location / {
proxy_pass http://172.16.1.7:8080;
}
}
[root@lb01 conf.d]# systemctl enable nginx
[root@lb01 conf.d]# systemctl start nginx
存在問題
抓包結果
從圖中可以看出,當我們只用proxy_pass
代理的時候,會發現如下問題:
10.0.0.1請求10.0.0.5的時候使用的是域名
10.0.0.5請求10.0.0.7的時候使用的是IP:port
之前課程中講到,當訪問80端口的時候,沒有域名的情況下,默認會去找排在最上面的那個配置文件。
所以我們需要解決這個問題,保留住最開始的請求頭部信息。
proxy_set_header
,這個模塊可以幫我們解決這個問題
配置文件如下:
[root@lb01 conf.d]# cat proxy_web_node1.conf
server {
listen 80;
server_name nginx.oldboy.com;
location / {
proxy_pass http://172.16.1.7:8080;
proxy_set_header Host $http_host;
}
}
使用http 1.1協議
[root@lb01 conf.d]# cat proxy_web_node1.conf
server {
listen 80;
server_name nginx.oldboy.com;
location / {
proxy_pass http://172.16.1.7:8080;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
}
}
記錄客戶端來源IP
在生產環境中,我們必須要記錄客戶端的來源IP,如果所有的訪問日誌,全都來源於代理,那麼我們根本不知道都有哪些地區的用戶訪問了我們什麼頁面。
還是使用proxy_set_header
優化配置文件如下:
[root@lb01 conf.d]# cat proxy_web_node1.conf
server {
listen 80;
server_name nginx.oldboy.com;
location / {
proxy_pass http://172.16.1.7:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
}
}
配置代理實戰
#要求如下
1.部署web01,wordpress ,zh
2.用遠端的數據庫
3.配置代理
blog.gjy.com 訪問:web01的wordpress
zh.gjy.com 訪問:web01的zh
兩種方式,都能訪問到對應的網站
1.加請求頭
2.不加請求頭
1.環境準備
外網IP | 內網IP | 主機名 |
---|---|---|
10.0.0.5 | 172.16.1.5 | lb01 |
10.0.0.7 | 172.16.1.7 | web01 |
10.0.0.51 | 172.16.1.51 | db01 |
部署web01
1)在web01上安裝nginx和php
[root@web01 ~]# rz -E
[root@web01 ~]# tar xf nginx_php_mariadb.tar.gz
[root@web01 ~]# ll
[root@web01 ~]# cd nginx_php_mariadb
[root@web01 nginx_php_mariadb]# yum install -y ./*rpm
2)創建www用戶
[root@web01 nginx_php_mariadb]# groupadd www -g 666
[root@web01 nginx_php_mariadb]# useradd www -u 666 -g 666 -s /sbin/nologin -M
3)修改nginx和PHP啓動用戶
[root@web01 php]# vim /etc/nginx/nginx.conf
user www;
[root@web01 php]# vim /etc/php-fpm.d/www.conf
user = www
group = www
4)編輯nginx配置文件wordpress和zh
[root@web01 php]# vim /etc/nginx/conf.d/blog.gjy.com.conf
server {
listen 80;
server_name blog.gjy.com;
root /code/wordpress;
index index.php index.html;
access_log /var/log/nginx/blog.drz.com_access.log main;
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 php]# vim /etc/nginx/conf.d/zh.gjy.com.conf
server {
listen 80;
server_name zh.gjy.com;
root /code/zh;
index index.php index.html;
access_log /var/log/nginx/zh.drz.com_access.log main;
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5)創建站點目錄並授權
[root@web01 php]# mkdir -p /code/{wordpress,zh}
6)代碼上線,上傳安裝包
[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@web01 code]# unzip WeCenter_3-3-3.zip
7)創建出用戶上傳目錄
[root@web01 code]# mkdir /code/wordpress/wp-content/uploads
[root@web01 php]# chown -R www.www /code/
8)啓動nginx和php並加入開機自啓
[root@web01 code]# systemctl start nginx php-fpm
[root@web01 code]# systemctl enable nginx php-fpm
部署數據庫db01
9)安裝數據庫
[root@db01 ~]# yum install -y mariadb-server
10)啓動數據庫
[root@db01 ~]# systemctl start mariadb
11)設置數據庫root用戶的密碼
[root@db01 ~]# mysqladmin -uroot password '1'
12)免交互創建數據庫
#創建數據庫
[root@db01 ~]# mysqladmin -uroot -p1 create wordpress
[root@db01 ~]# mysql -uroot -p1 -e 'create database zh'
#查看數據庫
[root@db01 ~]# mysql -uroot -p1 -e 'show databases'
13)免交互創建程序連接MySQL用戶
[root@db01 ~]# mysql -uroot -p1 -e "grant all on wordpress.* to wp@'172.16.1.%' identified by '1'"
[root@db01 ~]# mysql -uroot -p1 -e "grant all on zh.* to zh@'172.16.1.%' identified by '1'"
14)測試遠程連接數據庫
#遠程連接數據庫wordpress庫
[root@web01 ~]# mysql -uwp -p1 -h172.16.1.51
#遠程連接數據庫zh庫
[root@web01 ~]# mysql -uzh -p1 -h172.16.1.51
代理測試
1)web01部署網站
#準備配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/proxy.conf
server {
listen 80;
server_name proxy.drz.com;
location / {
root /code/proxy;
index index.html;
}
}
#創建站點目錄
[root@web01 ~]# mkdir /code/proxy
#部署代碼
[root@web01 ~]# echo 'web01...' > /code/proxy/index.html
#重啓nginx
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# nginx -s reload
#不加請求頭,會訪問到code第一個目錄,也就是搭建的博客頁面
#加請求頭,會訪問到自定義的 web01...
代理實戰
在lb01上安裝nginx
[root@lb01 php]# rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm
1)配置代理
加請求頭
[root@lb01 ~]# vim /etc/nginx/conf.d/daili.conf
server {
listen 80;
server_name blog.gjy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header HOST $http_host;
}
}
[root@lb01 conf.d]# vim zh.conf
server {
listen 80 ;
server_name zh.gjy.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header HOST $http_host;
}
}
不加請求頭,修改端口
#zh修改端口爲8888,對應的web服務器也需要修改端口爲8888
[root@lb01 conf.d]# vim zh.conf
server {
listen 80 ;
server_name zh.gjy.com;
location / {
proxy_pass http://10.0.0.7:8888;
}
}
[root@web01 conf.d]# vim zh.gjy.com.conf
server {
listen 8888;
...
}
2)創建www用戶
[root@lb01 ~]# groupadd www -g 666
[root@lb01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
3)修改nginx 啓動用戶
4)啓動nginx
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# nginx -是reload
小結:1.不加請求頭,端口會衝突的情況下,會默認訪問到/code目錄下第一個目錄配置文件。
2.加請求頭,會精確訪問到所指定的目錄
3.不加請求頭,指定端口,會精確訪問到端口所指定的配置文件位置
注意
反向代理模塊http_proxy_module
七層
location / {
proxy_pass http://172.16.1.7;
#調用模塊
include proxy_params;
}
#可以把應用模塊都編輯到一個文件中,調用
proxy_params
proxy_set_header HOST $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
chaoshi shijian x3;
configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# nginx -是reload
小結:1.不加請求頭,端口會衝突的情況下,會默認訪問到/code目錄下第一個目錄配置文件。
2.加請求頭,會精確訪問到所指定的目錄
3.不加請求頭,指定端口,會精確訪問到端口所指定的配置文件位置
注意
反向代理模塊http_proxy_module
七層
location / {
proxy_pass http://172.16.1.7;
#調用模塊
include proxy_params;
}
#可以把應用模塊都編輯到一個文件中,調用
proxy_params
proxy_set_header HOST $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
chaoshi shijian x3;