/ 中存储网

Nginx http代理服务器设置方法

2014-04-17 12:50:01 来源:kejihao
如何利用Nginx架设Http代理服务器

Nginx 一般都是作为web服务器和反向代理服务器存在的,我们也可以把它用作一个Http代理服务器。操作很简单,只需要修改一下它的配置文件nginx.conf 添加一个Server字段。

server {

listen 8080;

location / {

proxy_pass http://$http_host$request_uri;

access_log off;

}

}

另外还需要在http字段内,用resolver指定一下DNS服务器,否则会发生 “nginx 502 bad gateway” 的错误。我采用是Google提供的DNS服务:

resolver 8.8.8.8;

修改完之后需要重启一下nginx服务器:

nginx -s reload

最后,修改一下浏览器代理设置的,访问www.ip.cn

,就能看到IP地址发生变化了。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

resolver 8.8.8.8;

sendfile        on;

#tcp_nopush     on;

#keepalive_timeout  0;

keepalive_timeout  65;

#gzip  on;

server {

listen       8080;

#server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

#location / {

#    root   html;

#    index  index.html index.htm;

#}

location / {

proxy_pass http://$http_host$request_uri;

access_log off;

}

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

同时使用两个代理端口的配置

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

resolver 8.8.8.8;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;

#tcp_nopush     on;

#keepalive_timeout  0;

keepalive_timeout  65;

#gzip  on;

server {

listen 9999;

location / {

proxy_pass http://$http_host$request_uri;

access_log off;

}

}

server {

listen       8080;

#server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

#location / {

#    root   html;

#    index  index.html index.htm;

#}

location / {

proxy_pass http://$http_host$request_uri;

access_log off;

}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ /.php$ {

#    proxy_pass   http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ /.php$ {

#    root           html;

#    fastcgi_pass   127.0.0.1:9000;

#    fastcgi_index  index.php;

#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

#    include        fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ //.ht {

#    deny  all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

listen       8000;

listen       somename:8080;

server_name  somename  alias  another.alias;

#    location / {

root   html;

index  index.html index.htm;

}

#}

# HTTPS server

#

#server {

listen       443;

server_name  localhost;

#    ssl                  on;

ssl_certificate      cert.pem;

ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

ssl_prefer_server_ciphers   on;

#    location / {

root   html;

index  index.html index.htm;

}

#}

}