PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] How can i configure my nginx to make my web app be accessed by both ip and domain name?

 September 01, 2022     devops, nginx, nginx-reverse-proxy     No comments   

Issue

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;
}
}


Solution

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.



Answered By - rosaqq
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing