标签:tag name 协议 style linu length listen 反向代理服务器 eval
nginx正反向代理配置详解代理服务器系统环境为:centos7.3
nginx代理服务器为:192.168.10.10
测试客户端为局域网内任意windows电脑或Linux电脑
nginx不仅可以做反向代理,还能用作正向代理来进行上网等功能。如果把局域网外的Internet想象成一个巨大的资源库,则局域网中的客户端要访问Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理(也就是大家常说的,通过正向代理进行上网功能)
现在的网站基本上都是https,要解决既能访问http80端口也能访问https443端口的网站,需要配置两个SERVER节点,一个处理HTTP转发,另一个处理HTTPS转发,而客户端都通过HTTP来访问代理,通过访问代理不同的端口,来区分HTTP和HTTPS请求。
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
server {
resolver 114.114.114.114; #指定DNS服务器IP地址
listen 80;
location / {
proxy_pass http://$host$request_uri; #设定代理服务器的协议和地址
proxy_set_header HOST $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}
server {
resolver 114.114.114.114; #指定DNS服务器IP地址
listen 443;
location / {
proxy_pass https://$host$request_uri; #设定代理服务器的协议和地址
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
}
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
http的访问测试
[root@localhost ~]# curl -I --proxy 192.168.10.10:80 www.baidu.com
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 15:37:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 31 May 2018 09:28:16 GMT
Connection: keep-alive
ETag: "5b0fc030-264"
Accept-Ranges: bytes
https的访问测试
[root@localhost ~]# curl -I --proxy 192.168.10.10:443 www.baidu.com
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 15:38:07 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
[root@localhost ~]# vim /etc/profile
export http_proxy='192.168.10.10:80'
export http_proxy='192.168.10.10:443'
export ftp_proxy='192.168.10.10:80'
[root@localhost ~]# source /etc/profile
[root@localhost ~]# curl -I www.baidu.com:80
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 16:10:18 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
[root@localhost ~]# curl -I www.baidu.com:443
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 11 Jun 2018 16:10:27 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
上面结果就说明我们的服务端nginx正向代理和客户端使用nginx做为全局代理设置成功。
系统环境:centos7.3
代理服务器(nginx):192.168.10.10
后端服务器(apache):192.168.10.20
测试机:随便一台Linux或Windows都可以
反向代理:指将自己作为代理服务器来接受Internet上的请求,然后将请求转发给后端的服务器,并将结果返回给客户端。而nginx是一个高性能的http和反向代理服务器,且占用内存极少,现在很多大型公司都在用它。
加到nginx配置文件http标签里面
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
upstream test {
server 192.168.10.20;
}
server {
listen 80;
server_name 192.168.10.20;
index index.html index.htm;
location / {
proxy_pass http://test;
}
}
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo '192.168.10.20' > /var/www/html/index.html
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# curl 192.168.10.10
192.168.10.20
标签:tag name 协议 style linu length listen 反向代理服务器 eval
原文地址:http://blog.51cto.com/longlei/2154851