Nginx多Server反向代理配置
Nginx強(qiáng)大的正則表達(dá)式支持,可以使server_name的配置變得很靈活,如果你要做多用戶博客,那么每個(gè)用戶擁有自己的二級域名也就很容易實(shí)現(xiàn)了。
下面我就來說說server_name的使用吧:
server_name的匹配順序
Nginx中的server_name指令主要用于配置基于名稱虛擬主機(jī),server_name指令在接到請求后的匹配順序分別為:
1、準(zhǔn)確的server_name匹配,例如:
- server {
 - listen 80;
 - server_name ssdr.info www.ssdr.info;
 - ...
 - }
 
2、以*通配符開始的字符串:
- server {
 - listen 80;
 - server_name *.ssdr.info;
 - ...
 - }
 
3、以*通配符結(jié)束的字符串:
- server {
 - listen 80;
 - server_name www.*;
 - ...
 - }
 
4、匹配正則表達(dá)式:
- server {
 - listen 80;
 - server_name ~^(?.+)\.howtocn\.org$;
 - ...
 - }
 
Nginx將按照1,2,3,4的順序?qū)erver name進(jìn)行匹配,只有有一項(xiàng)匹配以后就會停止搜索,所以我們在使用這個(gè)指令的時(shí)候一定要分清楚它的匹配順序(類似于location指令)。
server_name指令一項(xiàng)很實(shí)用的功能便是可以在使用正則表達(dá)式的捕獲功能,這樣可以盡量精簡配置文件,畢竟太長的配置文件日常維護(hù)也很不方便。下面是2個(gè)具體的應(yīng)用:
在一個(gè)server塊中配置多個(gè)站點(diǎn):
- server
 - {
 - listen 80;
 - server_name ~^(www\.)?(.+)$;
 - index index.php index.html;
 - root /data/wwwsite/$2;
 - }
 
站點(diǎn)的主目錄應(yīng)該類似于這樣的結(jié)構(gòu):
- /data/wwwsite/ssdr.info
 - /data/wwwsite/linuxtone.org
 - /data/wwwsite/baidu.com
 - /data/wwwsite/google.com
 
這樣就可以只使用一個(gè)server塊來完成多個(gè)站點(diǎn)的配置。
在一個(gè)server塊中為一個(gè)站點(diǎn)配置多個(gè)二級域名 。
實(shí)際網(wǎng)站目錄結(jié)構(gòu)中我們通常會為站點(diǎn)的二級域名獨(dú)立創(chuàng)建一個(gè)目錄,同樣我們可以使用正則的捕獲來實(shí)現(xiàn)在一個(gè)server塊中配置多個(gè)二級域名:
- server
 - {
 - listen 80;
 - server_name ~^(.+)?\.howtocn\.org$;
 - index index.html;
 - if ($host = ssdr.info){
 - rewrite ^ http://www.ssdr.info permanent;
 - }
 - root /data/wwwsite/ssdr.info/$1/;
 - }
 
站點(diǎn)的目錄結(jié)構(gòu)應(yīng)該如下:
- /data/wwwsite/ssdr.info/www/
 - /data/wwwsite/ssdr.info/nginx/
 
這樣訪問www.ssdr.info時(shí)root目錄為/data/wwwsite/ssdr.info/www/,nginx.ssdr.info時(shí)為/data/wwwsite/ssdr.info/nginx/,以此類推。
后面if語句的作用是將ssdr.info的方位重定向到www.ssdr.info,這樣既解決了網(wǎng)站的主目錄訪問,又可以增加seo中對www.ssdr.info的域名權(quán)重。
多個(gè)正則表達(dá)式
如果你在server_name中用了正則,而下面的location字段又使用了正則匹配,這樣將無法使用$1,$2這樣的引用,解決方法是通過set指令將其賦值給一個(gè)命名的變量:
- server
 - {
 - listen 80;
 - server_name ~^(.+)?\.howtocn\.org$;
 - set $www_root $1;
 - root /data/wwwsite/ssdr.info/$www_root/;
 - location ~ .*\.php?$ {
 - fastcgi_pass 127.0.0.1:9000;
 - fastcgi_index index.php;
 - fastcgi_param SCRIPT_FILENAME /data/wwwsite/ssdr.info/$fastcgi_script_name;
 - include fastcgi_params;
 - }
 - }
 
Nginx不同域名反向代理到另一臺服務(wù)器 proxy_pass和$host
想讓一個(gè)VPS專門做另一個(gè)VPS的前端,后端VPS每添加一個(gè)域名,前端VPS就要同時(shí)添加一個(gè)域名來反向代理,作為前端的VPS如果一個(gè)一個(gè)的添加后端VPS的域名,那么這個(gè)事情特別麻煩,能不能讓其自動(dòng)反向代理后端VPS呢,用到proxy_pass和$host就可以輕松實(shí)現(xiàn)。
以下例子為了省事,以lnmp為安裝環(huán)境進(jìn)行設(shè)置
修改前端VPS的nginx.conf文件,修改成以下內(nèi)容:
- server {
 - listen 80;
 - server_name $host;
 - location / {
 - proxy_pass http://www.31.gd/;
 - proxy_set_header Host $host;
 - proxy_redirect off;
 - proxy_set_header X-Real-IP $remote_addr;
 - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 - proxy_connect_timeout 60;
 - proxy_read_timeout 600;
 - proxy_send_timeout 600;
 - }
 
下面的一并修改吧。
- location /.(php|php5)?$
 - {
 - fastcgi_pass unix:/tmp/php-cgi.sock;
 - fastcgi_index index.php;
 - include fcgi.conf;
 - }
 - location /status {
 - stub_status on;
 - access_log off;
 - }
 - location /.(gif|jpg|jpeg|png|bmp|swf)$
 - {
 - expires 30d;
 - }
 - location /.(js|css)?$
 - {
 - expires 12h;
 - }
 
這樣就可以實(shí)現(xiàn)了前端VPS可以反向代理任意域名到后端VPS,只要將域名解析到前端VPS,后端VPS進(jìn)行域名綁定,那么就可以直接訪問到了
一臺nginx帶多個(gè)域名多個(gè)tomcat情況的配置
多個(gè)域名,其中2個(gè)域名需支持泛域名解析:
1、www.abc.com
2、www.bcd.com
3、*.efg.com
4、*.hij.com
其中1,2,3為一臺tomcat,4為獨(dú)立tomcat。前端一臺nginx,通過配置多個(gè)虛擬主機(jī)來實(shí)現(xiàn)該部署。
進(jìn)入/etc/nginx/conf.d目錄,所有虛擬主機(jī)的配置文件都在該目錄下存放,配置。
配置支持泛域名
- #
 - # A virtual host using mix of IP-, name-, and port-based configuration
 - #
 - server {
 - listen 81;
 - server_name *.efg.com;
 - location / {
 - proxy_pass http://localhost:8080;
 - proxy_set_header Host $host;
 - proxy_set_header X-Real-IP $remote_addr;
 - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 - }
 - }
 - #
 - # A virtual host using mix of IP-, name-, and port-based configuration
 - #
 - server {
 - listen 81;
 - server_name *.hij.com;
 - location / {
 - proxy_pass http://localhost:8081;
 - proxy_set_header Host $host;
 - proxy_set_header X-Real-IP $remote_addr;
 - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 - }
 - }
 
泛域名解析關(guān)鍵為紅色部分,如果沒有紅色部分,后端8080及8081口對應(yīng)的tomcat虛擬主機(jī)將無法獲得域名信息,導(dǎo)致后端tomcat無法獲取到對應(yīng)的域名信息。
后端TOMCAT支持泛域名解析時(shí),需要設(shè)置 host name 為 localhost 以支持泛域名指向。
Nginx 多域名配置
nginx綁定多個(gè)域名可又把多個(gè)域名規(guī)則寫一個(gè)配置文件里,也可又分別建立多個(gè)域名配置文件,我一般為了管理方便,每個(gè)域名建一個(gè)文件,有些同類域名也可又寫在一個(gè)總的配置文件里。
一、每個(gè)域名一個(gè)文件的寫法
首先打開 nginx域名配置文件存放目錄:/usr/local/nginx/conf/servers ,如要綁定域名www.web126.com 則在此目錄建一個(gè)文件:www.web126.com.conf 然后在此文件中寫規(guī)則,如:
- server
 - {
 - listen 80;
 - server_name www.web126.com; #綁定域名
 - index index.htm index.html index.php; #默認(rèn)文件
 - root /home/www/web126.com; #網(wǎng)站根目錄
 - include location.conf; #調(diào)用其他規(guī)則,也可去除
 - }
 
然后重起nginx服務(wù)器,域名就綁定成功了。
Nginx服務(wù)器重起命令:/etc/init.d/nginx restart。
二、一個(gè)文件多個(gè)域名的寫法
一個(gè)文件添加多個(gè)域名的規(guī)則也是一樣,只要把上面單個(gè)域名重復(fù)寫下來就ok了,如:
- server
 - {
 - listen 80;
 - server_name www.web126.com; #綁定域名
 - index index.htm index.html index.php; #默認(rèn)文件
 - root /home/www/web126.com; #網(wǎng)站根目錄
 - include location.conf; #調(diào)用其他規(guī)則,也可去除
 - }
 - server
 - {
 - listen 80;
 - server_name msn.web126.com; #綁定域名
 - index index.htm index.html index.php; #默認(rèn)文件
 - root /home/www/msn.web126.com; #網(wǎng)站根目錄
 - include location.conf; #調(diào)用其他規(guī)則,也可去除
 - }
 
三、不帶www的域名加301跳轉(zhuǎn)
如果不帶www的域名要加301跳轉(zhuǎn),那也是和綁定域名一樣,先綁定不帶www的域名,只是不用寫網(wǎng)站目錄,而是進(jìn)行301跳轉(zhuǎn),如:
- server
 - {
 - listen 80;
 - server_name web126.com;
 - rewrite ^/(.*) http://www.web126.com/$1 permanent;
 - }
 
四、添加404網(wǎng)頁
添加404網(wǎng)頁,都可又直接在里面添加,如:
- server
 - {
 - listen 80;
 - server_name www.web126.com; #綁定域名
 - index index.htm index.html index.php; #默認(rèn)文件
 - root /home/www/web126.com; #網(wǎng)站根目錄
 - include location.conf; #調(diào)用其他規(guī)則,也可去除
 - error_page 404 /404.html;
 - }
 
***還有一個(gè)方法需要注意,可能有需要禁止IP直接訪問80端口或者禁止非本站的域名綁定我們的IP,這樣的話應(yīng)該
如下處理,放到最前一個(gè)server上面即可:
- server{
 - listen 80 default;
 - server_name _;
 - return 403;
 - }
 
學(xué)會上面四種規(guī)則方法,基本就可以自己獨(dú)立解決nginx 多域名配置問題了。















 
 
 








 
 
 
 