nginx的常見問題


Nginx常見問題

Nginx多Server優先級

准備nginx對應的配置文件

[root@web01 conf.d]# cat server1.conf 
server {
    listen 80;
    server_name localhost test1.com;

    location / {
        root /code/test1;
        index index.html;
    }
}
[root@web01 conf.d]# cat server2.conf 
server {
    listen 80;
    server_name localhost test2.com;

    location / {
        root /code/test2;
        index index.html;
    }
}
[root@web01 conf.d]# cat server3.conf 
server {
    listen 80;
    server_name localhost test3.com;

    location / {
        root /code/test3;
        index index.html;
    }
}
[root@web01 conf.d]#

准備站點目錄

[root@web01 conf.d]# mkdir /code/test{1..3}
[root@web01 conf.d]# echo test1 > /code/test1/index.html
[root@web01 conf.d]# echo test2 > /code/test2/index.html
[root@web01 conf.d]# echo test3 > /code/test3/index.html

檢查語法提示沖突,忽略並重啟

[root@web01 conf.d]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
[root@web01 conf.d]#

#根據ip訪問
#1. 用戶第一次訪問,讀取server1.conf配置返回結果
[root@lb01 ~]# curl 10.0.0.5
test1

#2. 此時將server1.conf修改為server4.conf重啟nginx
[root@lb01 conf.d]# mv server1.conf server4.conf
[root@lb01 conf.d]# nginx -s reload

#3. 再次訪問時,讀取server2.conf配置返回結果
[root@lb01 conf.d]# curl 10.0.0.5
test2

多Server_name優先級總結

再開始處理一個HTTP請求時,Nginx會讀取header(請求頭)中的host,與每個server中的server_name進行匹配,來決定用哪一個server標簽來完成處理這個請求,有可能一個Host與多個server中的server_name都匹配,這個時候就會根據匹配優先級來選擇實際處理的server。優先級匹配結果如下:

1.首先選擇所有的字符串完全匹配的server_name。(完全匹配)
2.選擇通配符在前面的server_name,如.haoda.com www.haoda.com3.選擇通配符在后面的server_name,如bgx. haoda.com haoda.cn
4.最后選擇使用正則表達式匹配的server_name
5.如果全部都沒有匹配到,那么將選擇在listen配置項后加入[default_server]的server塊
6.如果沒寫,那么就找到匹配listen端口的第一個Server塊的配置文件

注意:當出現多個相同的server_name情況下,配置文件排序優先使用則會被調用,所以建議配置相同端口,不同域名,這樣不會出現域名訪問沖突。

Nginx禁止IP直接訪問

當用戶通過訪問IP或者未知域名訪問你得網站的時候,你希望禁止顯示任何有效內容,可以給他返回500,目前國內很多機房都要求網站關閉空主機頭,防止未備案的域名指向過來造成麻煩

Nginx禁止IP訪問

[root@lb01 conf.d]# cat server4.conf 
server {
    listen 80 default_server;           #默認優先返回;
    server_name _;                      #空主機頭或者IP;
    return 500;                         #直接返回500錯誤;
}

引流的方式將訪問的IP直接跳轉主站域名

[root@lb01 conf.d]# cat server4.conf 
server {
    listen 80 default_server;
    server_name _;
    return 302 http://test1.com;    訪問10.0.0.5 就會跳轉test1.com
}

Nginx路徑root與alias

root與alias路徑匹配主要區別在於nginx如何解釋location后面的uri,這會使兩者分別以不同的方式將請求映射到服務器文件上,alias是一個目錄別名的定義,root則是最上層目錄的定義。

root的處理結果是:root路徑+location路徑alias的處理結果是:使用alias定義的路徑

4.1 使用root時,用戶訪問http://image.com/picture/1.jpg時,實際上Nginx會到/code/picture/目錄下找1.jpg文件

[root@web01 conf.d]# cat image.conf 
server {
    listen 80;
    server_name image.zhp.com;

    location /images {
        root /code/img;
    }
}

[root@web01 conf.d]# mkdir /code/img/images -p
[root@web01 conf.d]# cd /code/img/
[root@web01 img]# ll
total 8
drwxr-xr-x 2 root root    6 Sep  2 19:27 images
[root@web01 img]# cd images/
[root@web01 images]# ll
total 0
[root@web01 images]# rz

[root@web01 images]# ll
total 28
-rw-r--r-- 1 root root 26765 Aug  5 14:55 4.jpg
[root@web01 images]# 

用alise
[root@lb01 conf.d]# cat image.conf 
server {
    listen 80;
    server_name image.zhp.com;

    location /images {
        root /code/img;
    }
}

線上一般配置

[root@web01 conf.d]# vim image.conf 
server {
    listen 80;
    server_name image.zhp.com;

    location / {
        root /code;
    }

    location ~* ^/(.*\.(png|jpg|gif))$ {
        alias /code/images/$1;
    }
}

Nginx try_file路徑匹配

nginx的try_file路徑匹配,Nginx會按順序檢查文件及目錄是否存在(根據 root 和 alias 指令設置的參數構造完整的文件路徑),並用找到的第一個文件提供服務。在元素名后面添加斜杠 / 表示這個是目錄。如果文件和目錄都不存在,Nginx會執行內部重定向,跳轉到命令的最后一個 uri 參數定義的 URI 中。

5.1 Nginx try_file配置實例1

#1. 配置nginx
[root@lb01 conf.d]# vim try.conf 
server {
    listen 80;
    server_name try.haoda.com;
    root /code;
    index index.html;

    location / {
        try_files $uri /404.html;
    }
}

#2. 創建實例目錄與文件
[root@lb01 conf.d]# echo try11111 > /code/index.html 
[root@lb01 conf.d]# echo '404 404 404' > /code/404.html

#3. 嘗試訪問try.haoda.com
[root@lb01 conf.d]# curl try.haoda.com
404 404 404
#由於訪問的是try.haoda.com,而$uri取得是域名后面我們寫的內容,它找不到,所以返回后面的內容,即404.html

#4. 嘗試訪問try.haoda.com/index.html
[root@lb01 conf.d]# curl try.haoda.com/index.html
try11111
#由於訪問的是try.haoda.com/index.html,而$uri取到了index.html所以返回/code/index.html的內容

#5. 修改配置為
location / {
    try_files $uri $uri/ /404.html;
}

#6. 再次嘗試訪問try.haoda.com
[root@lb01 conf.d]# curl try.haoda.com
try11111
#我們訪問的是try.haoda.com,而$uri我們沒有寫任何內容,於是他訪問的便是“空/”,即匹配到/code/index.html

舉例

location /images/ {
        try_files $uri $uri/ /404.html;
}

用戶請求try.haoda.com/images/image1.gif,Nginx 會首先通過用於這個 location,在本地目錄中查找這個文件。如果“image1.gif”文件不存在,Nginx 會查找“image1.gif/”目錄,即“try.haoda.com/images/image1.gif/”,如果都不存在,會重定向到“/404.html”

5.2 Nginx try_file配置實例2

[root@web02 ~]# yum install -y tomcat  安裝tomcat

#1. 配置nginx
[root@web01 conf.d]# cat try.conf 
server {
    listen 80;
    server_name try.haoda.com;
    root /code;
    index index.html;

    location / {
        try_files $uri $uri/ @java; #當$uri和$uri/都匹配不到時,由后端的java來進行處理,名字可自定義,但一定要加@
    }

    location @java {
    proxy_pass http://172.16.1.8:8080;          #配置后端tomcat
    }
}

#2. 配置后端tomcat
[root@web02 ~]# cd /usr/share/tomcat/webapps/ROOT
[root@web02 ROOT]# echo 'i am tomcat' > index.html
[root@web02 ROOT]# systemctl start tomcat

#3. 把文件都挪走
[root@lb01 code]# mv index.html index1.html /tmp/

#4. 測試訪問
[root@lb01 code]# curl http://try.haoda.com/index.html
i am tomcat

Nginx優雅顯示錯誤頁面

error_page錯誤日志

7.1 第一種配置情況(跳轉網絡地址)

#error_page配置的是http這種的網絡地址
[root@lb01 conf.d]# cat error.conf 
server {
    listen       80;
    server_name  www.haoda.com;
    root /code;
    #error_page  404  http://www.baidu.com;

    location / {
        index index.html;
        error_page  404  http://www.baidu.com;
    }    
}

7.2 第二種配置情況(跳轉本地地址)

[root@lb01 conf.d]# cat error.conf 
server {
    listen 80;
    server_name error.haoda.com;
    root /code;

    location / {
        index index.html;
    }

    #error_page  403 404  /404.jpg;

    error_page 403 404 /404.html;
    location = /404.html {
        root /code;
        index index.html;   
    }
}


免責聲明!

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



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