I used nginx as a reverse proxy in a simple express app. In my config file, I have used server ip(http://45.33.97.232/) as server_name.
but is there any way to avoid writing the actual server ip in this file without breaking anything?
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. Typically the name _ is used in nginx example config files but this can be any invalid domain name such as ! 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.
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. So the server_name can be set to two double quotes ("") to signify empty string:
server {
listen 80;
server_name "";
...
}
Note though that this does not function as a catch-all domain name like the _ above. Instead it catches requests with empty host header.