在nginx.conf的http塊中定義變量:
http {
...
set $server_ip_addr "192.168.1.140";
...
}
在server塊中使用變量替換IP地址:
server {
listen 7777;
server_name localhost;
charset utf-8;
location / {
root /ky/java/nginx/html/web;
}
location /login/ {
proxy_pass http://$server_ip_addr:8080/kykj/login/;
}
location /hls/ {
proxy_pass http://$server_ip_addr:18080/hls/;
}
...
}
然后nginx容器啟動的時候就報錯了:
2023/04/14 09:02:20 [emerg] 1#1: "set" directive is not allowed here in /etc/nginx/nginx.conf:33
nginx: [emerg] "set" directive is not allowed here in /etc/nginx/nginx.conf:33
原因:我不該把這句直接加到http塊下, 要這樣加到http塊下的server塊下的location里:
這樣就沒問題
但是我又另外報了一個錯誤:using uninitialized "server_ip_addr" variable
我另外記錄一下吧
其實在http塊中也能定義變量的,不過要像這樣用:文章來源:http://www.zghlxwxcb.cn/news/detail-644104.html
geo $server_ip_addr {
default 192.168.1.140;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-644104.html
到了這里,關于nginx.conf報錯:“set“ directive is not allowed here in xxx(http塊下不能用set定義變量)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!