简体   繁体   English

nginx:有没有办法避免在 nginx 配置文件中写入服务器 ip 地址?

[英]nginx: Is there any way to avoid wriring server ip address in nginx config file?

I used nginx as a reverse proxy in a simple express app.我在一个简单的快速应用程序中使用 nginx 作为反向代理。 In my config file, I have used server ip( http://45.33.97.232/ ) as server_name.在我的配置文件中,我使用了服务器 ip( http://45.33.97.232/ )作为 server_name。

but is there any way to avoid writing the actual server ip in this file without breaking anything?但是有什么方法可以避免在这个文件中写入实际的服务器 ip 而不会破坏任何东西?

server {
    listen 80 default_server;
    server_name 45.33.97.232;

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://45.33.97.232:3000; #port where you are serving your express app.

  }
}

Yes.是的。 By setting the default_server flag in the listen directive and setting the server name to any invalid domain name.通过在listen指令中设置default_server标志并将服务器名称设置为任何无效的域名。 Typically the name _ is used in nginx example config files but this can be any invalid domain name such as !通常名称_用于 nginx 示例配置文件,但这可以是任何无效的域名,例如! etc: ETC:

server {
    listen 80 default_server;
    server_name _;

    ...
}

See http://nginx.org/en/docs/http/server_names.html#miscellaneous_names for more info on this.有关更多信息,请参阅http://nginx.org/en/docs/http/server_names.html#miscellaneous_names


Note that there is one special server name that has an additional meaning.请注意,有一个特殊的服务器名称具有其他含义。 If you need to serve clients that don't send the Host header (eg. HTTP/1.0 clients) then the empty string is used to signal that this server block is the one that is meant to serve such clients.如果您需要为不发送Host header 的客户端提供服务(例如 HTTP/1.0 客户端),则使用空字符串表示该服务器块是为此类客户端提供服务的块。 So the server_name can be set to two double quotes ( "" ) to signify empty string:因此 server_name 可以设置为两个双引号 ( "" ) 以表示空字符串:

server {
    listen 80;
    server_name "";

    ...
}

Note though that this does not function as a catch-all domain name like the _ above.请注意,这并不像上面的_那样将 function 作为一个包罗万象的域名。 Instead it catches requests with empty host header.相反,它会捕获带有空主机 header 的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM