今天爱分享给大家带来Nginx如何配置代理【附代码】,希望能够帮助到大家。
Nginx的代理功能非常实用,例如一个没有公网IP的服务器想要访问远端web服务器,而它们并不相通,此时可以选择一台代理服务器作为跳板,代理服务器和web服务器相通,从而使服务器可以访问到远端web服务器。
[root@minglinux-01 ~] vim /usr/local/nginx/conf/vhost/proxy.conf //新建proxy.conf文件
1 server
2 {
3 listen 80;
4 server_name ask.apelearn.com; //定义要访问的域名
5
6 location /
7 {
8 proxy_pass http://121.201.9.155/; //proxy_pass指定要代
理的域名所在的服务器IP
9 proxy_set_header Host $host; //后面的三行为定义发往后端Web服务器的请求头
10 proxy_set_header X-Real-IP $remote_addr;
11 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12 }
13 }
测试结果:
[root@minglinux-01 ~] /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#
User-agent: *
Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
[root@minglinux-01 ~] curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#
User-agent: *
Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/