nginx之轉(zhuǎn)發(fā)規(guī)則、負載均衡、server_name用法及說明
更新時間:2026年06月16日 11:10:04 作者:北風(fēng)toto
Nginx轉(zhuǎn)發(fā)規(guī)則詳解,包括ginx負載均衡機制,Nginx隱藏版本號配置,涵蓋location精確與正則匹配,Nginx服務(wù)器配置技巧全面解析
一、nginx常用的轉(zhuǎn)發(fā)規(guī)則
location 指令說明
該指令用于匹配 URL,語法如下:
| 指令 | 說明 |
|---|---|
| = | 用于不含正則表達式的 uri 前,要求請求字符串與 uri 嚴格匹配,如果匹配 成功,就停止繼續(xù)向下搜索并立即處理該請求。 |
| ~ | 用于表示 uri 包含正則表達式,并且區(qū)分大小寫。 |
| ~* | 用于表示 uri 包含正則表達式,并且不區(qū)分大小寫。 |
| ^~ | 用于不含正則表達式的uri前,要求Nginx服務(wù)器找到標識uri和請求字 符串匹配度最高的 location 后,立即使用此 location 處理請求,而不再使用 location 塊中的正則 uri 和請求字符串做匹配。 |
| !~ | 區(qū)分大小寫不匹配。 |
| !~* | 不區(qū)分大小寫不匹配 |
| /a | 普通前綴匹配,優(yōu)先級低于帶參數(shù)前綴匹配。 |
| / | 任何請求都會匹配 |
- 首先匹配 =
- 其次匹配^~,
- 其次是按文件中順序的正則匹配
- 最后是交給 / 通用匹配
- 當有匹配成功時候,停止匹配,按當前匹配規(guī)則處理請求。
location轉(zhuǎn)發(fā)使用
- location /api1/
# 如果請求的是:http://localhost:80/api1,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/
location /api1/ {
proxy_pass http://localhost:9001/;
}
- location = /api1/ (精確匹配)
# 如果請求的是:http://localhost:80/api1,會被匹配到
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/
# 如果請求的是:http://localhost:80/api1/test,不會被匹配到,因為是精確匹配
location = /api1/ {
proxy_pass http://localhost:9001/;
}
- location ~ /api1 (正則表達式匹配)
# rewrite重寫了請求路徑,break不可省略,$1為正則匹配的內(nèi)容
# ^/api1/(.*)$,在這個正則表達式中,$1為(.*)中的內(nèi)容
# proxy_set_header的使用并不會失效
# 如果請求的是:http://localhost:80/api1/test,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/test
location ~ /api1 {
rewrite ^/api1/(.*)$ /$1 break;
proxy_set_header test001 $host:$server_port;
proxy_set_header test002 $remote_addr;
proxy_pass http://localhost:9001;
}
# 如果請求的是:http://localhost:80/api1/test,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/api1/test
location ~ /api1 {
proxy_pass http://localhost:9001;
}
二、upstream負載均衡使用
- 當有其中一個服務(wù)掛了,并不會影響使用。nginx發(fā)現(xiàn)超時之后會使用其他能用的服務(wù),當訪問到掛了的服務(wù)響應(yīng)會延遲。當所有服務(wù)超時則報錯
#server只能是ip+端口,不然啟動報錯
upstream api{
server localhost:9001;
server localhost:9002;
server localhost:9003;
}
#proxy_pass里面的api對應(yīng)的是upstream后面的api
location /api/ {
proxy_pass http://api/;
}
三、server_name使用
- 看下列代碼,端口一樣,server_name不一樣
- 訪問http://www.test001.com/api/test,進入第一個server,轉(zhuǎn)發(fā)的實際為http://localhost:9001/test
- 訪問http://www.test002.com/api/test,進入第二個server,轉(zhuǎn)發(fā)的實際為http://localhost:9002/test
- 對于沒有配置的server_name,默認進入第一個server處理
- 訪問http://127.0.0.1/api/test,進入第一個server,轉(zhuǎn)發(fā)的實際為http://localhost:9001/test
- 訪問http://www.test003.com/api/test,進入第一個server,轉(zhuǎn)發(fā)的實際為http://localhost:9001/test
#本機host配置 127.0.0.1 www.test001.com 127.0.0.1 www.test002.com 127.0.0.1 www.test003.com
server {
#監(jiān)聽端口
listen 80;
#服務(wù)名
server_name www.test001.com;
location / {
root html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:9001/;
}
#500類型錯誤處理
error_page 500 502 503 504 /50x.html;
#映射文件50x.html
location = /50x.html {
#相對路徑
root html;
}
}
server {
#監(jiān)聽端口
listen 80;
#服務(wù)名
server_name www.test002.com;
location / {
root html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:9002/;
}
#500類型錯誤處理
error_page 500 502 503 504 /50x.html;
#映射文件50x.html
location = /50x.html {
#相對路徑
root html;
}
}
四、其他常用配置
限制請求類型
- 只允許GET和POST請求,寫在server塊
if($request_method !~ ^(GET|POST)$ ){
return 403;
}
處理靜態(tài)資源目錄遍歷問題
- 過濾…/ |…\,寫在server塊
if( $request_uri ~* \.\.[\\\\/] ){
return 404;
}
限制客戶端使用的ip或者域名
- 寫在server塊
#當寫127.0.0.1的時候,使用localhost會報500,只能使用127.0.0.1
if ( $host !~ ^127.0.0.1 ){
return 500;
}
五、需要注意的地方
- 當使用 location ~ 的時候, proxy_pass結(jié)尾不能為 / ,不然會報錯
- access_log需要寫在log_format后面,不然啟動會報錯。
- access_log只能打印出請求的路徑,無法打印出代理之后的路徑。
location /api1 探討
# 如果請求的是:http://localhost:80/api1,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/api1
location /api1 {
proxy_pass http://localhost:9001;
}
# 如果請求的是:http://localhost:80/api1,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/
location /api1/ {
proxy_pass http://localhost:9001/;
}
# 如果請求的是:http://localhost:80/api1,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001//
location /api1 {
proxy_pass http://localhost:9001/;
}
location ~ /api1 探討(正則表達式)
#proxy_pass最多只能寫到端口
#比如proxy_pass http://localhost:9001/,多個/報錯
#比如proxy_pass http://localhost:9001/test,多個/test報錯
#所以正則表達式的轉(zhuǎn)發(fā)經(jīng)常配合rewrite使用,但是不用正則表達式依舊可以使用。
# 如果請求的是:http://localhost:80/api1/test,
# 轉(zhuǎn)發(fā)形成的就會是:http://localhost:9001/api1/test
location ~ /api1 {
proxy_pass http://localhost:9001;
}
$host 和 $remote_addr 的區(qū)別
- $host 是客戶端使用的ip或者域名,$remote_addr是客戶端真正的ip
# $host為127.0.0.1 # $remote_addr為127.0.0.1 http://127.0.0.1/api/test # $host為www.test001.com # $remote_addr為127.0.0.1 http://www.test001.com/api/test # $host為localhost # $remote_addr為127.0.0.1 http://localhost/api/test # 假設(shè)我本機ip為192.168.1.27 # $host為www.baidu.com # $remote_addr為192.168.1.27 https://www.baidu.com/s?wd=北京
其他
Rewrite命令語法
rewrite < regex > < replacement > [flag] regex:正則表達式 replacement :跳轉(zhuǎn)后的內(nèi)容 flag:rewrite支持的flag標記
| flag標記說明 | |
|---|---|
| 標記 | 說明 |
| last | 相當于Apache的【L】標記,表示完成rewrite |
| break | 本條規(guī)則匹配完成即終止,不在匹配后面的任何規(guī)則 |
| redirect | 返回302臨時重定向,瀏覽器地址欄會顯示跳轉(zhuǎn)后的URL地址,爬蟲不會更新url |
| permanent | 返回301永久重定向,瀏覽器地址欄會顯示跳轉(zhuǎn)后的URL地址,爬蟲更新url |
| ast和break比較 | ||
|---|---|---|
| last | break | |
| 使用場景 | 一般寫在server和if中 | 一般使用在location中 |
| URL匹配 | 不重質(zhì)重寫后的url匹配 | 終止重寫后的url匹配 |
#$1為(.*) rewrite ^/api1/(.*)$ /$1 break; #$1為前面的(.*),$2為后面的(.*) rewrite ^/(.*)/(.*)$ /$1 break; #當正則表達式和請求不匹配的時候,后面的/$1將不被執(zhí)行,請求不會被rewrite替換 rewrite ^/apitest/(.*)$ /$1 break;
Nginx 中隱藏版本號
修改nginx.conf文件
打開 Nginx 配置文件,linux系統(tǒng)下一般在 /etc/nginx/nginx.conf 或 /usr/local/nginx/conf/nginx.conf。
在 http 塊中添加以下內(nèi)容:
server_tokens off;
修改fastcgi.conf文件
- 編輯php-fpm配置文件,如fastcgi.conf或fcgi.conf(這個配置文件名也可以自定義的,根據(jù)具體文件名修改)。


springboot 打印請求路徑
logging:
level:
org.springframework: debugspringboot打印controller被調(diào)用的方法
logging:
level:
org:
springframework:
web:
servlet:
mvc:
method:
annotation:
RequestMappingHandlerMapping: traceController獲取請求頭的內(nèi)容
HttpServletRequest request; Enumeration<String> enumeration= request.getHeaderNames();
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用nginx搭建RTMP視頻點播、直播、HLS服務(wù)器
本文主要介紹了利用nginx搭建RTMP視頻點播、直播、HLS服務(wù)器,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
nginx如何將http訪問的網(wǎng)站改成https訪問
這篇文章主要介紹了nginx如何將http訪問的網(wǎng)站改成https訪問,幫助大家更好的理解和使用nginx,感興趣的朋友可以了解下2021-02-02
實現(xiàn)nginx&php服務(wù)器配置的非主流配置方法
這種方法并非以前所流行的apache 加 php_module 的方式運行,我是采用nginx 作為web服務(wù)器,以fastcgi的方式運行php2011-05-05
Nginx代理proxy pass配置去除前綴的實現(xiàn)
這篇文章主要介紹了Nginx代理proxy pass配置去除前綴的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
分享nginx+php-fpm實現(xiàn)大文件下載排坑的過程
這篇文章主要介紹了nginx+php-fpm實現(xiàn)大文件下載排坑的過程,文中通過代碼實例相結(jié)合的形式給大家介紹的非常詳細,具有一定得參考借鑒價值,需要的朋友參考下吧2018-08-08

