How can I access my Nginx website via its IP address?

I have several websites on my linode LEMP stack (using nginx instead of apache). Each site lives in a separate folder /srv/www/sitename.com, where sitename is the name of the respective site.

I'm trying to migrate one site from it's previous host but I dont want to change the DNS records until the site is setup. However, I can't figure out how to access the site without it's domain name. I've tried IP-address/sitename.com but that just redirects me to my default_server.

Each domain has a configuration file in /etc/nginx/sites-available that looks something like this (below). What should I add to the configuration so that I can access the site with the ip address instead the sitename?:

server {

listen 80;

server_name www.sitename.com sitename.com;

access_log /srv/www/sitename.com/logs/access.log;

error_log /srv/www/sitename.com/logs/error.log;

root /srv/www/sitename.com/public_html;

index index.php;

rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;

rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

location ~ .php$ {

include /etc/nginx/fastcgi_params;

fastcgiintercepterrors on;

fastcgi_pass 127.0.0.1:9000;

fastcgi_pass php;

fastcgi_index index.php;

fastcgiparam SCRIPTFILENAME /srv/www/sitename.com/public_html

}

location = /favicon.ico {

lognotfound off;

access_log off;

}

location = /robots.txt {

allow all;

lognotfound off;

access_log off;

}

location / {

try_files $uri $uri/ /index.php?$args;

}

location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {

expires max;

lognotfound off;

}

}

Any help is appreciated! Thanks.

2 Replies

Edit your LOCAL computer's /etc/hosts file and add an entry for the IP and domain name you're trying to test. Your LOCAL computer uses this file FIRST when trying to resolve hostnames. This allows you to redirect your LOCAL computer to a different IP for a given hostname, without mucking with global DNS or server side changes.

Good luck!

-Chris

I have /etc/nginx/sites-available/default pointing to srv/www/default/publichtml, it loads a plain html file when the IP is placed in browser. To test a new site before DNS, I change root to "root /srv/www/sitename.com/publichtml;" to point the IP to the site I just migrated. When all is tested, I put default back and place /etc/nginx/sites-available/sitename.com so it's ready to go.

server {
listen 80;
server_name 123.45.67 my.fqdn.com;
access_log /srv/www/default/logs/access.log;
error_log /srv/www/default/logs/error.log;
root /srv/www/default/public_html;
...
server {
listen 80;
server_name 123.45.67 my.fqdn.com;
access_log /srv/www/default/logs/access.log;
error_log /srv/www/default/logs/error.log;
root /srv/www/sitename.com/public_html;
...

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct