nginx日常報錯解決


1. 問題一

報錯:

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /lingtian/opt/nginx/conf/vhost/test.conf:46

描述:

# 當nginx的location使用~或~*等正則表達式時,proxy_pass不能在后面等url中加 / 去掉location的內容,如:
location ~* /test {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $http_x_forwarded_for;
        proxy_set_header Host  $host;
        proxy_set_header Connection "";
        proxy_pass      http://127.0.0.1/;   # 想要通過此 / 去掉test后去請求http://127.0.0.1,如127.0.0.1/test/123 --> 127.0.0.1/123
}

解決:

# 1. 不使用正則表達式,如下:
location /test {    # 正則不允許,那就不用唄,但是這里不區分大小寫了,⚠️
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $http_x_forwarded_for;
        proxy_set_header Host  $host;
        proxy_set_header Connection "";
        proxy_pass      http://127.0.0.1/;   
}

# 2. 通過 $ 符號傳遞變量
location /test/(.*) {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $http_x_forwarded_for;
        proxy_set_header Host  $host;
        proxy_set_header Connection "";
        proxy_pass      http://127.0.0.1/$1;   # 此處$1是上面location中的括號內的內容
}

# 3. 通過lua來實現,這里不做介紹,有興趣自行學習


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM