nginx 常見問題一

2022-03-15 13:11:58 字數 2378 閱讀 8039

在開始處理乙個http請求時,nginx會取出header頭中的host變數,

與nginx.conf中的每個server_name進行匹配,

以此決定到底由哪乙個server來處理這個請求,

但nginx如何配置多個相同的server_name,會導致server_name出現優先順序訪問衝突。

[root@web01 /etc/nginx/conf.d]# vim test1.conf

server

}[root@web01 /etc/nginx/conf.d]# vim test2.conf

server

}[root@web01 /etc/nginx/conf.d]# vim test3.conf

server

}[root@web01 conf.d]#

[root@web01 /etc/nginx/conf.d]# mkdir /code/test

[root@web01 /etc/nginx/conf.d]# echo test1111111111111 > /code/test1/index.html

[root@web01 /etc/nginx/conf.d]# echo test2222222222222 > /code/test2/index.html

[root@web01 /etc/nginx/conf.d]# echo test3333333333333 > /code/test3/index.html

10.0.0.7 test1.com test2.com test3.com

#請求網域名稱時,可以請求到網域名稱對應的頁面,請求ip時,返回的頁面時配置檔案中第乙個配置的站點

# 在開始處理乙個http請求時,nginx會讀取header(請求頭)中的host(網域名稱),與每個server中的server_name進行匹配,

# 來決定用哪乙個server標籤來完成處理這個請求,有可能乙個host與多個server中的server_name都匹配,

# 這個時候就會根據匹配優先順序來選擇實際處理的server。優先順序匹配結果如下:

1.首先選擇所有的字串完全匹配的server_name。(完全匹配)

2.選擇萬用字元在前面的server_name,如 *.test.com (blog.test.com)

3.選擇萬用字元在後面的server_name,如 www.test.* (www.test.com www.test.cn)

4.最後選擇使用正規表示式匹配的server_name

5.如果全部都沒有匹配到,那麼將選擇在listen配置項後加入[default_server]的server塊

6.如果沒寫,那麼就找到匹配listen埠的第乙個server塊的配置檔案

#配置

[root@web01 /etc/nginx/conf.d]# cat server1.conf

server

}[root@web01 /etc/nginx/conf.d]# cat server2.conf

server

}[root@web01 /etc/nginx/conf.d]# cat server3.conf

server

}[root@web01 /etc/nginx/conf.d]# cat server4.conf

server

}[root@web01 /etc/nginx/conf.d]# cat server5.conf

server

}#站點檔案

[root@web01 /etc/nginx/conf.d]# echo linux.test.com > /code/server1/index.html

[root@web01 /etc/nginx/conf.d]# echo *.test.com > /code/server2/index.html

[root@web01 /etc/nginx/conf.d]# echo linux.test.* > /code/server3/index.html

[root@web01 /etc/nginx/conf.d]# echo '~^linux\.(.*)\.com$' > /code/server4/index.html

[root@web01 /etc/nginx/conf.d]# echo default_server > /code/server5/index.html

#配置hosts

10.0.0.7 linux.test.com

#訪問測試

壓縮配置,測試

壓縮配置,測試

nginx常見問題

1 顯示的錯誤資訊是 413 request entity too large 原因是請求實體太長了。一般出現種情況是post請求時body內容post的資料太大了,如上傳大檔案過大 如post資料比較多 在伺服器的 etc nginx目錄中找到nginx.conf 找到http 段,修改或增加 c...

Nginx常見問題

1.在 apache 的寫法 rewritecond nginx.org rewriterule 在 nginx 可以對應寫成 server 但 nginx 作者更建議的方法是 server server f q 1.請教一下,nginx的rewrite規則怎麼寫?比如將 http rewrite為...

Nginx常見問題

nginx常見問題 nginx優化主要從配置檔案著手,常見優化引數如下 worker processes 與cpu核心數量一致 worker connections 每個worker最大併發連線數 server tokens 伺服器版本號資訊 client header buffer size 預設...