Nginx实现正向代理

来源:互联网 发布:云海螺英语怎么样知乎 编辑:程序博客网 时间:2024/05/17 22:32

使用Nginx作为代理服务器,实现正向代理

Nginx服务器 IP:172.16.0.1

测试主机IP:192.168.0.1

测试主机为局域网内主机,不与外网相连,通过一台与外网相连的Nginx服务器,实现与外网的通信。

 1  2 #user  nobody;  3 worker_processes  1;  4  5 #error_log  logs/error.log;  6 #error_log  logs/error.log  notice;  7 #error_log  logs/error.log  info;  8  9 pid        logs/nginx.pid; 10 11 12 events { 13     worker_connections  1024; 14     #use epoll; 15 } 16 17 18 http { 19     include       mime.types; 20     default_type  application/octet-stream; 21 22     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 23     #                  '$status $body_bytes_sent "$http_referer" ' 24     #                  '"$http_user_agent" "$http_x_forwarded_for"'; 25 26     #access_log  logs/access.log  main; 27 28     sendfile        on; 29 30 31     #keepalive_timeout  0; 32     keepalive_timeout  65; 33 34     gzip  on; 35     proxy_temp_path /data0/proxy_temp_path; 36     proxy_cache_path /data0/proxy_cache_path  levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; 37 38     server { 39         listen   80; 40         resolver 10.3.9.5; 41         location / { 42         proxy_pass http://$http_host$request_uri; 43         proxy_redirect off; #关闭对发送给客户端的URL进行修改 44         proxy_set_header Host $http_host; 45         proxy_set_header X-Real-IP $remote_addr; # 把真实的ip 发送给 转发的web服务器 46         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; 47         proxy_cache cache_one; 48         proxy_buffers 256 4k; 49         proxy_max_temp_file_size 0; 50         proxy_connect_timeout 30; 51         proxy_cache_valid 301 302 1h; 52         proxy_cache_valid 200 304 1m; 53         proxy_cache_valid any 1m; 54         proxy_cache_key $host$uri$is_args$args; 55         allow 192.168.0.0/24; 56         deny all; 57         #charset koi8-r; 58 59        # access_log  logs/host.access.log  main; 60 61 62 63         } 64 65         #error_page  404              /404.html; 66 67         # redirect server error pages to the static page /50x.html 68         # 69         error_page   500 502 503 504  /50x.html; 70         location = /50x.html { 71             root   html; 72         } 73 74         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 75         # 76         #location ~ \.php$ { 77         #    proxy_pass   http://127.0.0.1; 78         #} 79 80         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 81         # 82         #location ~ \.php$ { 83         #    root           html; 84         #    fastcgi_pass   127.0.0.1:9000; 85         #    fastcgi_index  index.php; 86         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 87         #    include        fastcgi_params; 88         #} 89 90         # deny access to .htaccess files, if Apache's document root 91         # concurs with nginx's one 92         # 93         #location ~ /\.ht { 94         #    deny  all; 95         #} 96     } 97 }
测试过程:

原创粉丝点击