Nginx正向代理配置

一、nginx正向代理介绍及配置(需要在客户端配置代理服务器进行指定网站访问)

#模块 ngx_http_proxy_module:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

1、环境介绍

代理服务器系统环境为:centos

nginx代理服务器为:192.168.10.10

测试客户端为局域网内任意windows电脑或Linux电脑

2、正向代理简介

通过代理服务器来访问服务器的过程 就叫 正向代理。(常见示例,通过正向代理进行上网功能)

3、nginx正向代理的配置

3.1 http 80端口访问

3.2 https 443端口访问。

一个处理HTTP转发,另一个处理HTTPS转发,而客户端都通过HTTP来访问代理,通过访问代理不同的端口,来区分HTTP和HTTPS请求。

##/usr/local/nginx/conf/nginx.conf

server {

resolver 114.114.114.114; #resolver 定义域名解析。改成一个不存在的ip都不影响。

listen 80;

resolver_timeout 5s; #用于设置DNS服务器域名解析超时时间

access_log /usr/local/openresty/nginx/logs/access.log;

error_log /usr/local/openresty/nginx/logs/error.log;

location / {

proxy_redirect off;

proxy_pass http://$host$request_uri; #设定代理服务器的协议和地址

proxy_set_header HOST $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_buffers 256 4k; #配置缓存大小,关闭磁盘缓存读写减少I/O,以及代理连接超时时间。

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;

proxy_cache_valid 200 302 10m; #配置代理服务器 Http 状态缓存时间。

proxy_cache_valid 301 1h;

proxy_cache_valid any 1m;

client_max_body_size 100m;

client_body_buffer_size 128k;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_ignore_client_abort on;

}

}

server {

resolver 114.114.114.114; #指定DNS服务器IP地址

listen 443;

resolver_timeout 5s;

access_log /usr/local/openresty/nginx/logs/access.log;

error_log /usr/local/openresty/nginx/logs/error.log;

location / {

proxy_pass https://$http_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;

}

}

# /usr/local/nginx/sbin/nginx -s reload

4、Linux客户端访问测试

#http的访问测试

# 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的访问测试

# 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

5、设置Linux客户端全局代理

# 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'

# source /etc/profile

# 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

# 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做为全局代理设置成功。

6、取消代理unset http_proxy

相关文章

部署Nginx-一个ip,多个域名,部署多个项目

一、背景无论是在centos、ubuntu、iis等云服务器,还是在个人的电脑上,当部署Nginx时,基本上都会遇到想用一台服务器(只有一个IP)映射到多个域名上,以供访问多个不同的前端项目的情况(这...

在 Nginx 中防止 SSL 证书被其他域名“窜用”

在 Nginx 中防止 SSL 证书被其他域名“窜用”(即防止未经授权的域名使用你的 SSL 证书),需要确保你的服务器配置只允许合法域名访问,并通过适当的配置阻止非法域名的 HTTPS 请求。一般情...

搭建nginx反向代理用作内网域名转发

情景由于公司内网有多台服务器的http服务要映射到公司外网静态IP,如果用路由的端口映射来做,就只能一台内网服务器的80端口映射到外网80端口,其他服务器的80端口只能映射到外网的非80端口。非80端...

要将端口5002和5003通过Nginx代理到一个域名上的操作笔记

要将端口5002和5003通过Nginx代理到域名www.4rvi.cn的不同路径下,请按照以下步骤配置Nginx:步骤说明创建或编辑Nginx配置文件通常配置文件位于/etc/nginx/sites...

Nginx域名配置

对于想搭建自己博客或者其他个人网站的小伙伴,可能会需要配置域名。在我们购买完服务器,代码敲完,项目搞定,怎么使用域名访问呢?首先购买域名(要有DNS解析)-> 然后域名备案与审核 -> 最...

使用nginx配置动态域名解析

使用nginx配置来达到动态域名解析,resolver指令设置解析器的地址using-nginx-map-directive-to-dynamically-set-proxy-upstream要使用...