Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,用它来做代理访问网站,效果较好。下面以本地localhost访问本博客为例进行配置nginx反向代理服务器访问网站,请不要使用此技术做非法用途。采用的nginx版本为官方最新版1.21.3,系统为windows 10。nginx下载地址:http://nginx.org/en/download.html
下面是具体配置:
1、下载nginx,并解压到指定目录。
2、在conf目录中新建一个vhost目录,作为虚拟主机配置目录。
3、打开conf/nginx.conf,删除原有的Server,然后添加一行:include vhost/*.conf;具体配置如下:
#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; #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; # 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 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include vhost/*.conf; }
4、在conf/vhost下增加一个配置文件,localhost.conf,其配置如下:
server { listen 80; server_name localhost; resolver 129.226.164.124; location / { root html; index index.html index.htm Default.aspx; proxy_pass http://huikon.cn; proxy_connect_timeout 120; proxy_read_timeout 600; proxy_send_timeout 600; send_timeout 600; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
5、双击nginx.exe就可以启动了。
最后,赠送windows下nginx退出命令,如下:
nginx -s quit taskkill /im nginx.exe -f
windows下nginx重新加载配置命令,如下:
nginx -s reload
把上述命令做成bat批处理文件,可实现双击后执行命令。