Home > OS >  How can i configure my nginx to make my web app be accessed by both ip and domain name?
How can i configure my nginx to make my web app be accessed by both ip and domain name?

Time:09-27

I want to be able to access my web app by by both ip and domain.with my current config i can only access either one depending with what i put on the server name property server_name: ip|domain here is how my config is like

server {
server_name ip-address here;


location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_pass http://localhost:3000;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

CodePudding user response:

Like @Amin commented, you could just add your IP as a "server name" like:

server_name 171.233.45.4 www.mydomain.com;

However this is discouraged as the main point of a server name directive is to have multiple domains or subdomains hosted at the same IP address. I.e. you want to serve two apps at 171.233.45.4 and NGINX can distinguish them because one has server_name my_app.com; and the other server_name potatos.com;.

If you want access by IP then that indicates you are running only one service on that address port combination, meaning you do not need to specify a server name and instead could just configure DNS in the hosts file or in your router.

The simplest config would be:

server {
 listen 80;

 ...
}

Where 80 is the service port number. Then, you would be able to access the server by IP and hostname if you configure your hosts files or some sort of DNS service to point your hostname to the server's IP.

  • Related