nginx配置檔案參考
阿新 • • 發佈:2020-09-21
目錄
1.禁止ip訪問頁面
# 直接返回500錯誤 [root@web01 conf.d]# vim server4.conf server { listen 80 default_server; server_name localhost; return 500; } # 訪問ip跳轉到主頁 server { listen 80 default_server; server_name localhost; return 302 https://www.baidu.com; } # 返回指定內容 server { listen 80 default_server; server_name localhost; default_type text/plain; return 200 "頁面錯誤......."; }
2.多server優先順序
1.首先選擇所有的字串完全匹配的server_name。(完全匹配) 2.選擇萬用字元在前面的server_name,如*.mumusir.com www.mumusir.com 3.選擇萬用字元在後面的server_name,如mumusir.* mumusir.com mumusir.cn 4.最後選擇使用正則表示式匹配的server_name,如~^www\.(.*)\.com$ 5.如果全部都沒有匹配到,那麼將選擇在listen配置項後加入[default_server]的server塊 6.如果沒寫,那麼就找到匹配listen埠的第一個Server塊的配置檔案
3.nginx路徑的root和alias
root與alias路徑匹配主要區別在於nginx如何解釋location後面的uri,這會使兩者分別以不同的方式將請求對映到伺服器檔案上,alias是一個目錄別名的定義,root則是最上層目錄的定義。
root的處理結果是:root路徑+location路徑
alias的處理結果是:使用alias定義的路徑
[root@web01 conf.d]# vim image.conf server { listen 80; server_name image.com; location /picture { root /code; } } #使用root的時候,訪問http://image.com/picture/1.gif,實際上會到伺服器的/code/picture/目錄下面尋找1.gif檔案 [root@web01 conf.d]# vim image.conf server { listen 80; server_name image.com; location /picture { alias /code; } } #如果使用的是alias,訪問http://image.com/picture/1.gif,實際上是到伺服器的/code/目錄下尋找1.gif檔案 ######################## 生產中的配置 ######## server { listen 80; server_name image.com; location / { root /code; index index.html 1.html test.html } location ~* \.(jpg|png|gif)$ { alias /code/images; } }
4.Nginx的 try_file路徑匹配
nginx的try_file路徑匹配,Nginx會按順序檢查檔案及目錄是否存在(根據 root 和 alias 指令設定的引數構造完整的檔案路徑),並用找到的第一個檔案提供服務。在元素名後面新增斜槓 / 表示這個是目錄。如果檔案和目錄都不存在,Nginx會執行內部重定向,跳轉到命令的最後一個 uri 引數定義的 URI 中。
#配置nginx
[root@web01 conf.d]# vim try.conf
server {
listen 80;
server_name try.com;
location / {
root /code/try;
try_files $uri /404.html;
}
}
#建立目錄與檔案
[root@web01 conf.d]# mkdir /code/try
[root@web01 conf.d]# echo "try_file.index" > /code/try/index.html
[root@web01 conf.d]# echo 404040404 > /code/try/404.html
#訪問try.com,匹配$uri,而域名後面為空,匹配不到內容,所以匹配/404.html,返回的內容是/code/try/404.html
[root@web02 ~]# curl try.com
404040404
#訪問try.com/index.html,匹配$uri匹配到了/index.html,返回的內容就是/code/try/index.html
[root@web02 ~]# curl try.com/index.html
try_file.index
#修改nginx配置為
[root@web01 conf.d]# vim try.conf
server {
listen 80;
server_name try.com;
location / {
root /code/try;
try_files $uri $uri/ /404.html;
}
}
#訪問try.com,$uri匹配不到內容,交給$uri/匹配,匹配到的是 “空/”,所以訪問的連結是 try.com/ ,則能訪問到/code/try/index.html
############## 例項配置 ########################################
#1. 配置nginx
[root@lb01 conf.d]# cat try.conf
server {
listen 80;
server_name try.com;
root /code;
location / {
try_files $uri $uri/ @java; #當$uri和$uri/都匹配不到時,由後端的java來進行處理,名字可自定義,但一定要加@,內部子請求
}
location @java {
proxy_pass http://172.16.1.8:8080; #配置後端tomcat
}
}
5.優雅顯示錯誤頁面
# 遇到404錯誤跳轉到百度
server {
listen 80;
server_name error.linux.com;
location / {
root /code;
index index.html;
error_page 404 http://www.baidu.com;
}
}
# 跳轉到本地檔案
server {
listen 80;
server_name error.linux.com;
location / {
root /code;
index index.html;
error_page 404 /404.jpg;
}
}
# 訪問php錯誤頁面跳轉
server {
listen 80;
server_name blog.linux.com;
location / {
root /code/wordpress;
index index.php;
error_page 404 403 /404.jpg;
}
location ~* \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if (!-e $request_filename) {
rewrite (.*) /code/wordpress/404.jpg;
}
}
}