Nginx --day06
正向代理
反向代理
Nginx正向代理配置
Nginx正向代理使用场景并不多见。
需求场景1:
如果在机房中,只有一台机器可以联网,其他机器只有内网,内网的机器想用使用yum安装软件包,在能能联网的机器上配置一个正向代理即可。
Nginx正向代理配置文件
server {
listen 80 default_server;
resolver 119.29.29.29;
location /
{
proxy_pass http://$host$request_uri;
}
}
Nginx正向代理配置执行说明
resolver
语法:resolver address;
address为DNS服务器的地址,国内通用的DNS 119.29.29.29为dnspod公司提供。 国际通用DNS 8.8.8.8或者8.8.4.4为google提供。
其他可以参考 http://dns.lisect.com/

示例:resolver 119.29.29.29;
default_server
之所以要设置为默认虚拟主机,是因为这样就不用设置server_name了,任何域名解析过来都可以正常访问。
proxy_pass
该指令用来设置要代理的目标url,正向代理服务器设置就保持该固定值即可。关于该指令的详细解释在反向代理配置中。
[root@amingLiunx vhost]# !v
vim zp.conf
server {
listen 80 default_server;
resolver 119.29.29.29;
location /
{
proxy_pass http://$host$request_uri;
}
}
[root@amingLiunx vhost]# curl www.baidu.com
[root@amingLiunx vhost]# curl -x127.0.0.1:80 www.baidu.com ||都是可以curl通的
Nginx反向代理配置
Nginx反向代理在生产环境中使用很多的。
场景1:
域名没有备案,可以把域名解析到香港一台云主机上,在香港云主机做个代理,而网站数据是在大陆的服务器上。
示例1:
server
{
listen 80;
server_name aminglinux.com;

location /
{
proxy_pass http://123.23.13.11/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@amingLiunx vhost]# vi fp.conf
server
{
listen 80;
server_name www.test.com;

location /
{
proxy_pass http://127.0.0.1:8080/; ||127.0.0.1也可是是域名,但是需要host解析
proxy_set_header Host $host; ||如果没有后面这些,那么访问的时候,应该是访问到了上面的域名,意思最终访问的是host就是最上面的server_name
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@amingLiunx vhost]# vi test.com.conf
server
{
listen 8080 default_server;
server_name www.test.com;
root /data/wwwroot/test.com;
index index.html;
}
[root@amingLiunx vhost]# mkdir /data/wwwroot/test.com
[root@amingLiunx vhost]# echo "test.com_8080" >!$/index.html
echo "test.com_8080" >/data/wwwroot/test.com/index.html
[root@amingLiunx vhost]# curl -x127.0.0.1:8080 www.test.com
test.com_8080
[root@amingLiunx vhost]# curl 127.0.0.1:8080
test.com_8080
[root@amingLiunx vhost]# cp test.com.conf default_8080.conf
[root@amingLiunx vhost]# vi default_8080.conf
server
{
listen 8080 default_server;
root /data/wwwroot/test.com;
index index.html;
location /
{
echo "8080 default";
}
}
同时test.com.conf去掉default_server
[root@amingLiunx vhost]# curl 127.0.0.1:8080
8080 default
[root@amingLiunx vhost]# curl -x127.0.0.1:8080 www.test.com
test.com_8080
[root@amingLiunx vhost]# curl -x127.0.0.1:80 www.test.com
test.com_8080 ||至此,80端口和8080端口的结果一致,说明80已经代理了8080端口,简单的反向代理的原理
配置说明

  1. proxy_pass

在正向代理中,已经使用过该指令。
格式很简单: proxy_pass URL;
其中URL包含:传输协议(http://, https://等)、主机名(域名或者IP:PORT)、uri。
示例如下:
proxy_pass http://www.aminglinux.com/;
proxy_pass http://192.168.200.101:8080/uri;
proxy_pass unix:/tmp/www.sock;
对于proxy_pass的配置有几种情况需要注意。
示例2:
location /aming/
{
proxy_pass http://192.168.1.10;
...
}
环境准备:
环境准备
[root@amingLiunx vhost]# vi fp.conf
server
{
listen 80;
server_name www.test.com;
location /aming/
{
proxy_pass http://127.0.0.1:8080/;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
}
[root@amingLiunx vhost]# !v
vi default_8080.conf
server
{
listen 8080 default_server;
root /data/wwwroot/test.com;
index index.html;
location /aming/
{
echo "8080 default aming";
}
location /linux/
{
echo "8080 default linux";
}
}
[root@amingLiunx vhost]# vi /data/wwwroot/test.com/a.html
8080 a.html
[root@amingLiunx vhost]# cat /tmp/123.log
127.0.0.1 - - [21/Dec/2019:11:41:45 +0800] "GET /a.html HTTP/1.0" 200 14 "-" "curl/7.29.0"
[root@amingLiunx vhost]# vi fp.conf ||这一步去掉8080后面的/
[root@amingLiunx vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@amingLiunx vhost]# curl -x127.0.0.1:80 www.test.com/aming/a.html
8080 default aming
[root@amingLiunx vhost]# tail /tmp/123.log

127.0.0.1 - - [21/Dec/2019:11:41:45 +0800] "GET /a.html HTTP/1.0" 200 14 "-" "curl/7.29.0"

127.0.0.1 - - [21/Dec/2019:11:42:50 +0800] "GET /aming/a.html HTTP/1.0" 200 19 "-" "curl/7.29.0"
[root@amingLiunx vhost]# vi fp.conf ||这一步后面加个/linux
[root@amingLiunx vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@amingLiunx vhost]# curl -x127.0.0.1:80 www.test.com/aming/a.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@amingLiunx vhost]# cat fp.conf
server
{
listen 80;
server_name www.test.com;

location /aming/
{
proxy_pass http://127.0.0.1:8080/linux;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
}
[root@amingLiunx vhost]# vi fp.conf ||这一步后面加的是/linux/
[root@amingLiunx vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@amingLiunx vhost]# !curl
curl -x127.0.0.1:80 www.test.com/aming/a.html
8080 default linux
[root@amingLiunx vhost]# tail /tmp/123.log

127.0.0.1 - - [21/Dec/2019:11:41:45 +0800] "GET /a.html HTTP/1.0" 200 14 "-" "curl/7.29.0"

127.0.0.1 - - [21/Dec/2019:11:42:50 +0800] "GET /aming/a.html HTTP/1.0" 200 19 "-" "curl/7.29.0"
127.0.0.1 - - [21/Dec/2019:11:44:54 +0800] "GET /linuxa.html HTTP/1.0" 404 169 "-" "curl/7.29.0"
127.0.0.1 - - [21/Dec/2019:11:46:08 +0800] "GET /linux/a.html HTTP/1.0" 200 19 "-" "curl/7.29.0"
示例3:
location /aming/
{
proxy_pass http://192.168.1.10/;
...
}
示例4:
location /aming/
{
proxy_pass http://192.168.1.10/linux/;
...
}
示例5:
location /aming/
{
proxy_pass http://192.168.1.10/linux;
...
}
假设server_name为www.aminglinux.com
当请求http://www.aminglinux.com/aming/a.html的时候,以上示例2-5分别访问的结果是
示例2:http://192.168.1.10/aming/a.html
示例3:http://192.168.1.10/a.html
示例4:http://192.168.1.10/linux/a.html
示例5:http://192.168.1.10/linuxa.html

  1. proxy_set_header

proxy_set_header用来设定被代理服务器接收到的header信息。
语法:proxy_set_header field value;
field为要更改的项目,也可以理解为变量的名字,比如host
value为变量的值
如果不设置proxy_set_header,则默认host的值为proxy_pass后面跟的那个域名或者IP(一般写IP),
比如示例4,请求到后端的服务器上时,完整请求uri为:http://192.168.1.10/linux/a.html
如果设置proxy_set_header,如 proxy_set_header host $host;
比如示例4,请求到后端的服务器完整uri为:http://www.aminglinux.com/linux/a.html
proxy_set_header X-Real-IP $remote_addr;和proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
用来设置被代理端接收到的远程客户端IP,如果不设置,则header信息中并不会透传远程真实客户端的IP地址。
可以用如下示例来测试:
示例6(被代理端)
server{
listen 8080;
server_name www.aminglinux.com;
root /tmp/123.com_8080;
index index.html;
location /linux/ {
echo "$host";
echo $remote_addr;
echo $proxy_add_x_forwarded_for;
}
}
示例7(代理服务器上)
server {
listen 80;
server_name www.aminglinux.com;
location /aming/
{
proxy_pass http://192.168.1.10:8080/linux/;
proxy_set_header host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@amingLiunx vhost]# vim test.com.conf
server
{
listen 8080 ;
server_name www.test.com;
root /data/wwwroot/test.com;
index index.html;
location /
{
echo "$remote_addr";
echo "$proxy_add_x_forwarded_for";
}
[root@amingLiunx vhost]# vi fp.conf
server
{
listen 80;
server_name www.test.com;
location /aming/
{
proxy_pass http://127.0.0.1:8080/linux/;
proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
}
[root@amingLiunx vhost]# curl -x127.0.0.1:80 www.test.com/aming/a.html
127.0.0.1
127.0.0.1
[root@amingLiunx vhost]# curl -x192.168.0.106:80 www.test.com/aming/a.html
127.0.0.1
127.0.0.1
[root@amingLiunx vhost]# vi fp.conf
server
{
listen 80;
server_name www.test.com;
location /aming/
{
proxy_pass http://127.0.0.1:8080/linux/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@amingLiunx vhost]# curl -x192.168.0.106:80 www.test.com/aming/a.html
127.0.0.1
192.168.0.106, 127.0.0.1