歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Nginx 配置代理上網

Nginx 配置代理上網

日期:2017/3/1 13:51:33   编辑:關於Linux
1. Nginx簡介 Nginx是和Apache相似的, 以前實驗室的網絡是不受限制的, 而宿捨的網絡很多國外網站都打不開, 宿捨和實驗室都處於校園網, 這個就可以通過代理在宿捨通過實驗室網絡浏覽網頁. 這樣利用Nginx忘了算是正向代理還是反向代理了????

2. 下面是修改了的配置文件
# /etc/nginx/nginx.conf

####################################################################### # # This is the main Nginx configuration file. # # More information about the configuration options is available on # * the English wiki - http://wiki.nginx.org/Main # * the Russian documentation - http://sysoev.ru/nginx/ # #######################################################################
#---------------------------------------------------------------------- # Main Module - directives that cover basic functionality # # http://wiki.nginx.org/NginxHttpMainModule # #----------------------------------------------------------------------
user nginx; worker_processes 1;
error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;

#---------------------------------------------------------------------- # Events Module # # http://wiki.nginx.org/NginxHttpEventsModule # #----------------------------------------------------------------------
events { worker_connections 1024; }

#---------------------------------------------------------------------- # HTTP Core Module # # http://wiki.nginx.org/NginxHttpCoreModule # #----------------------------------------------------------------------
http { include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on; # # The default server # server { resolver 222.201.130.30; listen 82; server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / { proxy_pass http://$http_host$request_uri; }
error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; }
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/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; #} }
# Load config files from the /etc/nginx/conf.d directory include /etc/nginx/conf.d/*.conf;
}
3. 主要修改的地方 目前現狀:只有1個機器能上網(web),其他機器不能
方法:能上網的做一個代理web服務器中轉,其他機器連接它即可。采用nginx

Nginx配置如下:

server{
resolver x.x.x.x;
listen 82;
location / {
proxy_pass http://$http_host$request_uri; } }
注意項: 1. 不能有hostname 2. 必須有resolver, 即dns,即上面的x.x.x.x,換成你們的DNS服務器ip即可 3 . $http_host和$request_uri是nginx系統變量,不要想著替換他們,保持原樣就OK。
查看dns方法 cat /etc/resolv.conf
4. 代理使用 在linux, 我記得在當時是用的chrome浏覽器的一個插件, 填下代理服務器IP和端口就可以了.
在需要訪問外網的機器上執行以下操作之一即可: 1. export http_proxy=http://yourproxyaddress:proxyport 2. gedit ~/.bashrc export http_proxy=http://yourproxyaddress:proxyport yourproxyaddress也就是你的Nginx服務器的ip了,proxyport就是上面配置中的82,可以根據自己的需要修改。

舉例:
  1. worker_processes 1;
  2. master_process off;
  3. daemon off;
  4. #pid /var/run/nginx.pid;

  5. events {
  6. worker_connections 768;
  7. # multi_accept on;
  8. }

  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;

  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';

  15. access_log /var/log/nginx/access.log;
  16. error_log /var/log/nginx/error.log;

  17. sendfile on;

  18. server {
  19. resolver 10.57.220.2;
  20. listen 82;
  21. access_log logs/host.access.log main;

  22. location / {
  23. proxy_pass http://$http_host$request_uri;
  24. }


  25. }
  26. }









Copyright © Linux教程網 All Rights Reserved