nginx的常見問題

Nginx常見問題

Nginx多Server優先級html

準備nginx對應的配置文件java

[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]#

準備站點目錄nginx

[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

檢查語法提示衝突,忽略並重啓web

[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塊的配置文件vim

注意:當出現多個相同的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則是最上層目錄的定義。tomcat

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

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;   
    }
}
相關文章
相關標籤/搜索