iptables中實現內外網相互訪問 SNAT與DNAT的原理與應用
阿新 • • 發佈:2021-08-05
Nginx常見問題
Nginx 多server優先順序
在開始處理一個http請求時,nginx會取出header頭中的Host變數,與nginx.conf中的每個server_name進行匹配,
以此決定到底由哪一個server來處理這個請求,但nginx如何配置多個相同的server_name,會導致server_name出
現優先順序訪問衝突。
優先順序案例
[root@lb01 code]# cat /etc/nginx/conf.d/server*.conf server { listen 80; server_name localhost test1.com; location / { root /code/test1; index index.html; } } server { listen 80; server_name localhost test2.com; location / { root /code/test2; index index.html; } } server { listen 80; server_name localhost test3.com; location / { root /code/test3; index index.html; } } [root@lb01 conf.d]# cd /code/ [root@lb01 code]# ll total 0 drwxr-xr-x 2 root root 6 Aug 10 00:31 test1 drwxr-xr-x 2 root root 6 Aug 10 00:31 test2 drwxr-xr-x 2 root root 6 Aug 10 00:31 test3 [root@lb01 code]# echo test1 > /code/test1/index.html [root@lb01 code]# echo test2 > /code/test2/index.html [root@lb01 code]# echo test3 > /code/test3/index.html
server_name匹配順序
1.首先選擇所有的字串完全匹配的server_name。(完全匹配) 2.選擇萬用字元在前面的server_name,如*.driverzeng.com www.driverzeng.com 3.選擇萬用字元在後面的server_name,如driverzeng.* driverzeng.com driverzeng.cn 4.最後選擇使用正則表示式匹配的server_name 5.如果全部都沒有匹配到,那麼將選擇在listen配置項後加入[default_server]的server塊 6.如果沒寫,那麼就找到匹配listen埠的第一個Server塊的配置檔案
注意:當出現多個相同的server_name情況下,配置檔案排序優先使用則會被呼叫,所以建議配置相同埠,不同域 名,這樣不會出現域名訪問衝突。
nginx禁止IP解析
當用戶通過訪問IP或者未知域名訪問你得網站的時候,你希望禁止顯示任何有效內容,可以給他返回500,目前國內 很多機房都要求網站關閉空主機頭,防止未備案的域名指向過來造成麻煩
server {
listen 80 default_server; #預設優先返回;
server_name _; #空主機頭或者IP;
return 500; #直接返回500錯誤;
}
Nginx 的include使用
一臺伺服器配置多個網站,如果配置都寫在nginx.conf主配置檔案中,會導致nginx.conf主配置檔案變得非常龐大而 且可讀性非常的差。那麼後期的維護就變得麻煩。 假設現在希望快速的關閉一個站點,該怎麼辦? 1.如果是寫在 nginx.conf中,則需要手動註釋,比較麻煩 2.如果是include的方式,那麼僅需修改配置檔案的副檔名,即可完成注 釋 Include包含的作用是為了簡化主配置檔案,便於人類可讀。 inlcude /etc/nginx/online/*.conf #線上使用的配置 /etc/nginx/offline #保留配置,不啟用(下次使用在移動到online中)
Ninx的alias用法
root與alias路徑匹配主要區別在於nginx如何解釋location後面的uri,這會使兩者分別以不同的方式將請求對映到 伺服器檔案上,alias是一個目錄別名的定義,root則是最上層目錄的定義。 root的處理結果是:root路徑+location路徑alias的處理結果是:使用alias定義的路徑
server {
listen 80 default_server;
server_name _;
return 500;
}
server {
listen 80;
server_name www.test2.com;
location / {
root /code/test3;
index index.html;
}
location /images {
root /code/picture;
index index.html;
}
}
server {
listen 80 default_server;
server_name _;
return 500;
}
server {
listen 80;
server_name www.test2.com;
location / {
root /code/test3;
index index.html;
}
location /images {
alias /code/picture;
index index.html;
}
}
生產環境配置alias
server {
listen 80;
server_name image.driverzeng.com;
location / {
root /code;
}
location ~* ^.*\.(png|jpg|gif)$ {
alias /code/images/;
}
}
try_files使用
nginx的try_file路徑匹配,Nginx會按順序檢查檔案及目錄是否存在(根據 root 和 alias 指令設定的引數構造完整 的檔案路徑),並用找到的第一個檔案提供服務。在元素名後面新增斜槓 / 表示這個是目錄。如果檔案和目錄都不存 在,Nginx會執行內部重定向,跳轉到命令的最後一個 uri 引數定義的 URI 中。
#1. 配置nginx
[root@lb01 conf.d]# vim try.conf
server {
listen 80;
server_name try.drz.com;
root /code;
index index.html;
location / {
try_files $uri $uri/ /404.html;
}
}
#3. 嘗試訪問try.drz.com
[root@lb01 conf.d]# curl try.drz.com
404 404 404
#由於訪問的是try.drz.com,而$uri取得是域名後面我們寫的內容,它找不到,所以返回後面的內容,即
404.html
#4. 嘗試訪問try.drz.com/index.html
[root@lb01 conf.d]# curl try.drz.com/index.html
try11111
#由於訪問的是try.drz.com/index.html,而$uri取到了index.html所以返回/code/index.html的內容
#5. 修改配置為
location / {
try_files $uri $uri/ /404.html;
}
#6. 再次嘗試訪問try.drz.com
[root@lb01 conf.d]# curl try.drz.com
try11111
#我們訪問的是try.drz.com,而$uri我們沒有寫任何內容,於是他訪問的便是“空/”,即匹配
到/code/index.html
try_files 企業實戰2
server {
listen 80;
server_name try.drz.com;
root /code;
index index.html;
location / {
try_files $uri $uri/ @tomcat;
}
location @tomcat {
proxy_pass http://172.16.1.8:8080;
}
}
nginx優雅的訪問404
server {
listen 80 default_server;
server_name _;
return 500;
}
server {
listen 80;
server_name www.test2.com;
location / {
root /code/test3;
index index.html;
}
location /images {
alias /code/picture;
index index.html;
}
error_page 404 /404.html;
}
[root@lb01 conf.d]# vim /code/test3/404.html
<img style='width:100%;height:100%;'
src=https://blog.driverzeng.com/zenglaoshi/404_page.png>