site: http://www.nginx.com/prod.html
wiki: http://wiki.nginx.org/NginxChsHttpProxyModule
Install: http://wiki.nginx.org/Install
Articals:
http://www.cyberciti.biz/faq/rhel-linux-install-nginx-as-reverse-proxy-load-balancer/
http://www.ubuntugeek.com/using-nginx-as-a-reverse-proxy-to-get-the-most-out-of-your-vps.html
http://www.kutukupret.com/2011/05/11/nginx-reverse-proxying-multiple-domains-using-map-module/ (Map module)
在centos 5.x 和centos 6下的安装:
正常情况下,直接运行: yum install nginxj即可, 但是如果提示nginx package不存在,则先创建/etc/yum.repos.d/nginx.repo 文件
内容如下:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ gpgcheck=0 enabled=1
注意:Replace “OS
” with “rhel
” or “centos
”, depending on the distribution used, and “OSRELEASE
” with “5
” or “6
”, for 5.x or 6.x versions,
保存后,再次运行 yum install nginx即可。
配置:/etc/nginx/nginx.conf
user nginx; #默认用户 worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; server_names_hash_bucket_size 64; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; gzip off; # you should enable gzip within site #gzip_comp_level 5; #gzip_http_version 1.0; #gzip_min_length 0; #gzip_types text/plain text/html text/css image/x-icon application/x-javascript; $gzip_vary on; include /etc/nginx/conf.d/*.conf; #自动加载所有的配置文件 include /etc/nginx/sites-enabled/*; #自动加载所有的站点配置文件 }
配置/etc/nginx/conf.d/proxy.conf
proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; client_header_buffer_size 64k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 256k; proxy_buffers 32 16k; proxy_busy_buffers_size 256k;
泛域名站点文件配置: (假定domain是:abc.com)
例子: 配置*.testing.abc.com 全部转向到*.dev.abc.com
配置文件:/etc/nginx/sites-enabled/testing.ab.com
内容如下:(里面的abc.com 根据你自己的情况进行替换)
server { listen 80; server_name *.testing.abc.com; if ( $host ~* (.*).testing.abc.com) { set $sitename $1; } resolver 8.8.8.8; #因为我们自己没有配置dns,所以直接用google的dns access_log /var/log/nginx/$sitename.testing.abc.com-nginx.access.log; error_log /var/log/nginx/$sitename.testing.abc.com-nginx.error.log; index index.html; location / { proxy_pass http://$sitename.dev.abc.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www; } }
非泛域名配置:
内容如下:
server { listen 80; server_name abc.com www.abc.com *.abc.com; access_log /var/log/nginx/abc.com-nginx.access.log; error_log /var/log/nginx/abc.com-nginx_error.log debug; #redirect all non-www to www if ($host !~ 'www.abc.com' ) { rewrite ^/(.*)$ http://www.abc.com/$1 permanent; } #set your default location location / { proxy_pass http://www.anotherdomain.com; } #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 /var/www; } }
小插曲,因为服务器上已经默认安装了apache,导致nginx无法启动。 你可以使用lsof -i tcp:80 看看哪些程序占用了80端口.
[root@271334 ~]# lsof -i tcp:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 1465 root 3u IPv4 4095368955 0t0 TCP *:http (LISTEN) httpd 1466 apache 3u IPv4 4095368955 0t0 TCP *:http (LISTEN) httpd 1481 apache 3u IPv4 4095368955 0t0 TCP *:http (LISTEN) httpd 1752 apache 3u IPv4 4095368955 0t0 TCP *:http (LISTEN) httpd 4046 apache 3u IPv4 4095368955 0t0 TCP *:http (LISTEN) [root@271334 ~]# yum remove httpd Loaded plugins: fastestmirror Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package httpd.i686 0:2.2.15-5.el6.centos set to be erased --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: httpd i686 2.2.15-5.el6.centos installed 2.8 M Transaction Summary ================================================================================ Remove 1 Package(s) Reinstall 0 Package(s) Downgrade 0 Package(s) Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : httpd-2.2.15-5.el6.centos.i686 1/1 warning: /etc/httpd/conf/httpd.conf saved as /etc/httpd/conf/httpd.conf.rpmsave Removed: httpd.i686 0:2.2.15-5.el6.centos Complete! [root@271334 ~]#
创建/var/www目录和默认的index.html和50x.html文件.
index.html内容如下:
<html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx!</h1></center> </body> </html>
50x.html内容如下:
<html> <head> <title>The page is temporarily unavailable</title> <style> body { font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body bgcolor="white" text="black"> <table width="100%" height="100%"> <tr> <td align="center" valign="middle"> The page you are looking for is temporarily unavailable.<br/> Please try again later. </td> </tr> </table> </body> </html>
记得每次在更改配置文件,要重启nginx
/etc/init.d/nginx restart
enjoy with nginx. :)