Nginx二級域名及多Server反向代理配置


Nginx強大的正則表達式支持,可以使server_name的配置變得很靈活,如果你要做多用戶博客,那么每個用戶擁有自己的二級域名也就很容易實現了。

注:nginx反向代理同一ip多個域名,給header加上host就可以了

下面我就來說說server_name的使用吧:

server_name的匹配順序

Nginx中的server_name指令主要用於配置基於名稱虛擬主機,server_name指令在接到請求后的匹配順序分別為:

1、准確的server_name匹配,例如:

server { 
listen 80; 
server_name ssdr.info www.ssdr.info; 
... 

2、以*通配符開始的字符串:

server { 
listen 80; 
server_name *.ssdr.info; 
... 

3、以*通配符結束的字符串:

server { 
listen 80; 
server_name www.*; 
... 

4、匹配正則表達式:

server { 
listen 80; 
server_name ~^(?.+)\.howtocn\.org$; 
... 

Nginx將按照1,2,3,4的順序對server name進行匹配,只有有一項匹配以后就會停止搜索,所以我們在使用這個指令的時候一定要分清楚它的匹配順序(類似於location指令)。

server_name指令一項很實用的功能便是可以在使用正則表達式的捕獲功能,這樣可以盡量精簡配置文件,畢竟太長的配置文件日常維護也很不方便。下面是2個具體的應用:

在一個server塊中配置多個站點:

server 

listen 80; 
server_name ~^(www\.)?(.+)$; 
index index.php index.html; 
root /data/wwwsite/$2; 

站點的主目錄應該類似於這樣的結構:

/data/wwwsite/ssdr.info 
/data/wwwsite/linuxtone.org 
/data/wwwsite/baidu.com 
/data/wwwsite/google.com 

這樣就可以只使用一個server塊來完成多個站點的配置。

在一個server塊中為一個站點配置多個二級域名 。

實際網站目錄結構中我們通常會為站點的二級域名獨立創建一個目錄,同樣我們可以使用正則的捕獲來實現在一個server塊中配置多個二級域名:

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/; 

站點的目錄結構應該如下:

/data/wwwsite/ssdr.info/www/ 
/data/wwwsite/ssdr.info/nginx/ 

這樣訪問www.ssdr.info時root目錄為/data/wwwsite/ssdr.info/www/,nginx.ssdr.info時為/data/wwwsite/ssdr.info/nginx/,以此類推。

后面if語句的作用是將ssdr.info的方位重定向到www.ssdr.info,這樣既解決了網站的主目錄訪問,又可以增加seo中對www.ssdr.info的域名權重。

多個正則表達式

如果你在server_name中用了正則,而下面的location字段又使用了正則匹配,這樣將無法使用$1,$2這樣的引用,解決方法是通過set指令將其賦值給一個命名的變量:

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不同域名反向代理到另一台服務器 proxy_pass和$host

想讓一個VPS專門做另一個VPS的前端,后端VPS每添加一個域名,前端VPS就要同時添加一個域名來反向代理,作為前端的VPS如果一個一個的 添加后端VPS的域名,那么這個事情特別麻煩,能不能讓其自動反向代理后端VPS呢,用到proxy_pass和$host就可以輕松實現。

以下例子為了省事,以lnmp為安裝環境進行設置

修改前端VPS的nginx.conf文件,修改成以下內容:

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; 

這樣就可以實現了前端VPS可以反向代理任意域名到后端VPS,只要將域名解析到前端VPS,后端VPS進行域名綁定,那么就可以直接訪問到了

一台nginx帶多個域名多個tomcat情況的配置

多個域名,其中2個域名需支持泛域名解析:

1、www.abc.com

2、www.bcd.com

3、*.efg.com

4、*.hij.com

其中1,2,3為一台tomcat,4為獨立tomcat。前端一台nginx,通過配置多個虛擬主機來實現該部署。

進入/etc/nginx/conf.d目錄,所有虛擬主機的配置文件都在該目錄下存放,配置。

配置支持泛域名(二級域名)


# 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; 

泛域名解析關鍵為紅色部分,如果沒有紅色部分,后端8080及8081口對應的tomcat虛擬主機將無法獲得域名信息,導致后端tomcat無法獲取到對應的域名信息。

后端TOMCAT支持泛域名解析時,需要設置 host name 為 localhost 以支持泛域名指向。

Nginx 多域名配置

nginx綁定多個域名可又把多個域名規則寫一個配置文件里,也可又分別建立多個域名配置文件,我一般為了管理方便,每個域名建一個文件,有些同類域名也可又寫在一個總的配置文件里。

一、每個域名一個文件的寫法

首先打開 nginx域名配置文件存放目錄:/usr/local/nginx/conf/servers ,如要綁定域名www.web126.com 則在此目錄建一個文件:www.web126.com.conf 然后在此文件中寫規則,如:

server 

listen       80; 
server_name www.web126.com;             #綁定域名 
index index.htm index.html index.php;      #默認文件 
root /home/www/web126.com;               #網站根目錄 
include location.conf;                            #調用其他規則,也可去除 

然后重起nginx服務器,域名就綁定成功了。

Nginx服務器重起命令:/etc/init.d/nginx restart。

二、一個文件多個域名的寫法

一個文件添加多個域名的規則也是一樣,只要把上面單個域名重復寫下來就ok了,如:

server 

listen       80; 
server_name www.web126.com;             #綁定域名 
index index.htm index.html index.php;      #默認文件 
root /home/www/web126.com;               #網站根目錄 
include location.conf;                            #調用其他規則,也可去除 

server 

listen       80; 
server_name msn.web126.com;             #綁定域名 
index index.htm index.html index.php;      #默認文件 
root /home/www/msn.web126.com;        #網站根目錄 
include location.conf;                            #調用其他規則,也可去除 

三、不帶www的域名加301跳轉

如果不帶www的域名要加301跳轉,那也是和綁定域名一樣,先綁定不帶www的域名,只是不用寫網站目錄,而是進行301跳轉,如:

server 

listen 80; 
server_name web126.com; 
rewrite ^/(.*) http://www.web126.com/$1 permanent; 

四、添加404網頁

添加404網頁,都可又直接在里面添加,如:

  • 添加404網頁,都可又直接在里面添加,如:

    server 

    listen       80; 
    server_name www.web126.com;             #綁定域名 
    index index.htm index.html index.php;      #默認文件 
    root /home/www/web126.com;               #網站根目錄 
    include location.conf;                            #調用其他規則,也可去除 
    error_page 404  /404.html; 

最后還有一個方法需要注意,可能有需要禁止IP直接訪問80端口或者禁止非本站的域名綁定我們的IP,這樣的話應該

如下處理,放到最前一個server上面即可:

server{ 
listen   80 default; 
server_name      _; 
return 403; 

參考:http://os.51cto.com/art/201404/437182.htm

       http://www.xker.com/page/e2012/1124/122011.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM