Nginx的server、location用法及說明
server常用指令
listen指令
用來配置監(jiān)聽端口
| 語法 | listen address[:port] [default_server]...; listen port [default_server]...; |
| 默認(rèn)值 | listen *:80 | *:8000 |
| 位置 | server |
listen的設(shè)置比較靈活,我們通過幾個例子來把常用的設(shè)置方式熟悉下:
listen 127.0.0.1:8000; // listen localhost:8000 監(jiān)聽指定的IP和端口 listen 127.0.0.1; 監(jiān)聽指定IP的所有端口 listen 8000; 監(jiān)聽指定端口上的連接 listen *:8000; 監(jiān)聽指定端口上的連接
default_server
default_server屬性是標(biāo)識符,用來將此虛擬主機(jī)設(shè)置成默認(rèn)主機(jī)。
所謂的默認(rèn)主機(jī)指的是如果沒有匹配到對應(yīng)的address:port,則會默認(rèn)執(zhí)行的。
如果不指定默認(rèn)使用的是第一個server。
server{
listen 8080;
server_name 127.0.0.1;
location /{
root html;
index index.html;
}
}
server{
listen 8080 default_server;
server_name localhost;
default_type text/plain;
return 444 'This is a error request';
}server_name指令
用來設(shè)置虛擬主機(jī)服務(wù)名稱
| 語法 | server_name name ...; name可以提供多個中間用空格分隔 |
| 默認(rèn)值 | server_name ""; |
| 位置 | server |
關(guān)于server_name的配置方式有三種,分別是
- 精確匹配
- 通配符匹配
- 正則表達(dá)式匹配
精確匹配
server {
listen 80;
server_name www.test.cn www.test1.cn;
...
}使用通配符配置
server {
listen 80;
server_name *.test.cn www.test1.*;
...
}使用正則表達(dá)式配置
server_name中可以使用正則表達(dá)式,并且使用~作為正則表達(dá)式字符串的開始標(biāo)記
常見的正則表達(dá)式
| 代碼 | 說明 |
| ^ | 匹配搜索字符串開始位置 |
| $ | 匹配搜索字符串結(jié)束位置 |
| . | 匹配除換行符\n之外的任何單個字符 |
| \ | 轉(zhuǎn)義字符,將下一個字符標(biāo)記為特殊字符 |
| [xyz] | 字符集,與任意一個指定字符匹配 |
| [a-z] | 字符范圍,匹配指定范圍內(nèi)的任何字符 |
| \w | 與以下任意字符匹配 A-Z a-z 0-9 和下劃線,等效于[A-Za-z0-9_] |
| \d | 數(shù)字字符匹配,等效于[0-9] |
| {n} | 正好匹配n次 |
| {n,} | 至少匹配n次 |
| {n,m} | 匹配至少n次至多m次 |
| * | 零次或多次,等效于{0,} |
| + | 一次或多次,等效于{1,} |
| ? | 零次或一次,等效于{0,1} |
舉例
server{
listen 80;
server_name ~^www\.(\w+)\.com$; # 以 "www." 開頭,后跟一個或多個單詞字符(字母、數(shù)字、下劃線),然后以 ".com" 結(jié)尾的域名。其中,( ) 括號用于捕獲匹配的內(nèi)容,可以在后面的指令中使用。
default_type text/plain; # 設(shè)置響應(yīng)的 MIME 類型為 "text/plain",即純文本類型
return 200 $1 $2 ..; # 將捕獲到的第一個括號內(nèi)的值 $1、第二個括號內(nèi)的值 $2 依次附加在返回的文本內(nèi)容中。
}
注意 ~后面不能加空格,括號可以取值由于server_name指令支持通配符和正則表達(dá)式,因此在包含多個虛擬主機(jī)的配置文件中,可能會出現(xiàn)一個名稱被多個虛擬主機(jī)的server_name匹配成功,當(dāng)遇到這種情況,當(dāng)前的請求交給誰來處理呢?
server{
listen 80;
server_name ~^www\.\w+\.com$;
default_type text/plain;
return 200 'regex_success';
}
server{
listen 80;
server_name www.test.*;
default_type text/plain;
return 200 'wildcard_after_success';
}
server{
listen 80;
server_name *.test.com;
default_type text/plain;
return 200 'wildcard_before_success';
}
server{
listen 80;
server_name www.test.com;
default_type text/plain;
return 200 'exact_success';
}
server{
listen 80 default_server;
server_name _;
default_type text/plain;
return 444 'default_server not found server';
}那么他們都會去執(zhí)行一遍
exact_success wildcard_before_success wildcard_after_success regex_success default_server not found server!!
location
用來設(shè)置請求的URI
| 語法 | location [ = | ~ | ~* | ^~ |@ ] uri{...} |
| 默認(rèn)值 | |
| 位置 | server,location |
Nginx 的 locaiton 作?是根據(jù)?戶請求的 URI 不同,來執(zhí)行不同的應(yīng)用。針對用戶請求的網(wǎng)站URL 進(jìn)行匹配,匹配成功后進(jìn)行對應(yīng)的操作。
location [ = | ~| ~* | ^~ ] uri {
#指定對應(yīng)的動作
}?uri變量是待匹配的請求字符串,可以不包含正則表達(dá)式,也可以包含正則表達(dá)式,那么nginx服務(wù)器在搜索匹配location的時候,是先使用不包含正則表達(dá)式進(jìn)行匹配,找到一個匹配度最高的一個,然后在通過包含正則表達(dá)式的進(jìn)行匹配,如果能匹配到直接訪問,匹配不到,就使用剛才匹配度最高的那個location來處理請求。
正則表達(dá)式解釋
| 匹配符 | 匹配規(guī)則 | 優(yōu)先級 |
| = | 精確匹配 | 1 |
| ^~ | 以某個字符串開頭,不做正則 | 2 |
| ~* | 正則匹配 | 3 |
| /blog/ | 匹配常規(guī)字符串,有正則就優(yōu)先正則 | 4 |
| / | 通?匹配,不符合其他location的默認(rèn)匹配 | 5 |
不帶符號,要求必須以指定模式開始
server {
listen 80;
server_name 127.0.0.1;
location /abc{
default_type text/plain;
return 200 "access success";
}
}
以下訪問都是正確的
http://192.168.2.4/abc
http://192.168.2.4/abc?p1=TOM
http://192.168.2.4/abc/
http://192.168.2.4/abcdef= : 用于不包含正則表達(dá)式的uri前,必須與指定的模式精確匹配
server {
listen 80;
server_name 127.0.0.1;
location =/abc{
default_type text/plain;
return 200 "access success";
}
}
可以匹配到
http://192.168.2.4/abc
http://192.168.2.4/abc?p1=TOM
匹配不到
http://192.168.2.4/abc/
http://192.168.2.4/abcdef~ : 用于表示當(dāng)前uri中包含了正則表達(dá)式,并且區(qū)分大小寫
~*: 用于表示當(dāng)前uri中包含了正則表達(dá)式,并且不區(qū)分大小寫
server {
listen 80;
server_name 127.0.0.1;
location ~^/abc\w${
default_type text/plain;
return 200 "access success";
}
}
server {
listen 80;
server_name 127.0.0.1;
location ~*^/abc\w${
default_type text/plain;
return 200 "access success";
}
}^~: 用于不包含正則表達(dá)式的uri前,功能和不加符號的一致,唯一不同的是,如果模式匹配,那么就停止搜索其他模式了。
server {
listen 80;
server_name 127.0.0.1;
location ^~/abc{
default_type text/plain;
return 200 "access success";
}
}root/alias指令
root
設(shè)置請求的根目錄
| 語法 | root path; |
| 默認(rèn)值 | root html; |
| 位置 | http、server、location |
path為Nginx服務(wù)器接收到請求以后查找資源的根目錄路徑。
alias
用來更改location的URI
| 語法 | alias path; |
| 默認(rèn)值 | |
| 位置 | location |
path為修改后的根路徑。
以上兩個指令都可以來指定訪問資源的路徑,那么這兩者之間的區(qū)別是什么?
舉例說明 :在/usr/local/nginx/html目錄下創(chuàng)建一個 images目錄,并在目錄下放入一張圖片mv.png圖片
location /images {
root /usr/local/nginx/html;
}
root的訪問圖片的路徑為
http://192.168.2.4/images/mv.png
如果把root改為alias,再次訪問上述地址,頁面會出現(xiàn)404的錯誤
需要在alias后面路徑改為
location /images {
alias /usr/local/nginx/html/images;
}小結(jié)
root的處理結(jié)果是: root路徑+location路徑
/usr/local/nginx/html/images/mv.png
alias的處理結(jié)果是:使用alias路徑替換location路徑
/usr/local/nginx/html/images
index指令
設(shè)置網(wǎng)站的默認(rèn)首頁
| 語法 | index file ...; |
| 默認(rèn)值 | index index.html; |
| 位置 | http、server、location |
index后面可以跟多個設(shè)置,如果訪問的時候沒有指定具體訪問的資源,則會依次進(jìn)行查找,找到第一個為止。
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
訪問該location的時候,可以通過 http://ip:port/,地址后面如果不添加任何內(nèi)容,則默認(rèn)依次訪問index.html和index.htm,找到第一個來進(jìn)行返回error_page指令
設(shè)置網(wǎng)站的錯誤頁面
| 語法 | error_page code ... [=[response]] uri; |
| 默認(rèn)值 | |
| 位置 | http、server、location...... |
當(dāng)出現(xiàn)對應(yīng)的響應(yīng)code后,如何來處理。
1.可以指定具體跳轉(zhuǎn)的地址
server {
error_page 404 http://www.itcast.cn;
}2.可以指定重定向地址
server{
error_page 404 /50x.html;
error_page 500 502 503 504 /50x.html;
location =/50x.html{
root html;
}
}3.使用location的@符合完成錯誤信息展示
server{
error_page 404 @jump_to_error;
location @jump_to_error {
default_type text/plain;
return 404 'Not Found Page...';
}
}實(shí)戰(zhàn)
修改 nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# gzip on;
server {
listen 83;
server_name _;
#最低級匹配,不符合其他locaiton就來這
location / {
return 401;
}
#優(yōu)先級最?
location = / {
return 402;
}
#以/blog/開頭的url,來這?,如符合其他locaiton,則以其他優(yōu)先
location /blog/ {
return 403;
}
#匹配任何以/img/開頭的請求,不匹配正則
location ^~ /img/ {
return 404;
}
}
}
訪問localhost:83/

訪問localhost:83/blog/

訪問localhost:83/img/

rewrire
Nginx rewrire技術(shù)主要是實(shí)現(xiàn)URL地址重寫,且?持正則表達(dá)式的規(guī)則。 通過rewrite可以規(guī)范URL、根據(jù)變量進(jìn)?URL跳轉(zhuǎn)等,常?的功能如
- 對于爬?的封禁,讓其跳轉(zhuǎn)無用頁面
- 動態(tài)的URL偽裝成HTMl??,便于搜索引擎的抓取
- 舊域名、舊?錄的更新,需要跳轉(zhuǎn)到新的URL地址
語法
rewrite ^/(.*) http://www.baidu.com/$1 permanent;
rewrite是指令,開啟?個跳轉(zhuǎn)規(guī)則
正則是 ^/(.*) 表示匹配所有,匹配成功后跳轉(zhuǎn)到后?的url地址
$1 表示取出前?正則括號?的內(nèi)容
permanent表示 301 重定向的標(biāo)記
參數(shù)解析
| 標(biāo)記 | 解釋 |
| last | 規(guī)則匹配完成后,繼續(xù)向下匹配新的 Locaiton |
| break | 本條規(guī)則完成匹配后,?即停? |
| redirect | 返回 302 臨時重定向,瀏覽器地址欄顯示跳轉(zhuǎn)后的 URL |
| permanent | 返回 301 永久重定向,瀏覽器地址顯示跳轉(zhuǎn)后的 URL |
- last和break?于實(shí)現(xiàn)URL重寫,瀏覽器地址欄不發(fā)?變化
- redirect和permanent?于實(shí)現(xiàn)URL跳轉(zhuǎn),瀏覽器地址欄跳轉(zhuǎn)新的URL
訪問localhost:90/

跳轉(zhuǎn)百度

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx訪問靜態(tài)資源配置的實(shí)現(xiàn)步驟
Nginx 擅長于底層服務(wù)器端資源的處理,例如靜態(tài)資源處理轉(zhuǎn)發(fā)、反向代理,負(fù)載均衡等,本文主要介紹了Nginx訪問靜態(tài)資源配置的實(shí)現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2023-09-09
nginx+lua單機(jī)上萬并發(fā)的實(shí)現(xiàn)
nginx是我們最常用的服務(wù)器,常用于做內(nèi)容分發(fā)和反向代理,本文主要介紹了nginx+lua單機(jī)上萬并發(fā)的實(shí)現(xiàn),有興趣的可以了解下2021-05-05
Nginx+RTMP+nginx-http-flv-module環(huán)境搭建
本文主要介紹了Nginx+RTMP+nginx-http-flv-module環(huán)境搭建,搭建方式可用于直播、視頻會議等場景,同時支持HTTP-FLV,方便在瀏覽器中進(jìn)行播放2024-03-03
nginx轉(zhuǎn)發(fā)squid代理實(shí)現(xiàn)方式
本文介紹了如何使用nginx的stream模塊將客戶端請求轉(zhuǎn)發(fā)至squid服務(wù)器,首先概述了所需的服務(wù)器環(huán)境配置,接著詳細(xì)描述了nginx配置stream模塊與http模塊的方法,強(qiáng)調(diào)二者是平行關(guān)系,最后提供了具體的配置示例,并強(qiáng)調(diào)這些內(nèi)容是作者的經(jīng)驗(yàn)總結(jié)2026-04-04
解讀nginx -s reload會產(chǎn)生什么影響
這篇文章主要介紹了nginx -s reload會產(chǎn)生什么影響,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-07-07
nginx location中多個if里面proxy_pass的方法
這篇文章主要介紹了nginx location中多個if里面proxy_pass的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

