I am using nginx, until now I was running my php applications on /usr/share/nginx/html which is the default document root when nginx is installed on centOS and like systems.

Now I had to create server blocks (or virtual hosts) to run another application, followed this tutorial (https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts) and ended up creating my application in /var/www/html.

On this process, as the tutorial states to edit etc/hosts to your public IP, this made another application run that is deployed on my LAN. I wanted it to run on my machine only, so I changed it to my local IP and made the following entry to hosts dev.mysite.com www.dev.mysite.com 

Strangely, just after that I noticed my hostname had changed to

muUsername@dev instead of @localhost before
  1. Please tell me why it happened and how to change it back.
  2. Now the application runs when I access localhost in the browser, but not when I access dev.mysite.com, How to fix that?
  3. On accessing www.dev.mysite.com I get my old application at the old webroot (uss/share/nginx/html) Why is this happening and how to fix this?

Need any more information, please comment.


1) It happens because you are setting up a FQDN for your machine and the hostname is assumed to be the subdomain part in this case. That's due to the behaviour of gethostname function. If you run hosname -f it will give you the FQDN and hostname -d will give you the domain name. Set your machine's name before all these FQDN.

Update : Didn't pay attention you were on centos, updated the second file location

/etc/hosts : mymachine dev.mysite.com www.dev.mysite.com  



Then reboot.

2) When you hit that domain name locally, the kernel will look for /etc/hosts content at some point and then route the request to your loopback interface. You probably missed a server name in your nginx configuration.

3) Change your document root path for this server name.

server {
    listen 80;
    server_name www.dev.mysite.com;
    root /var/www/dev.mysite.com;

server {
    listen 80 default_server;
    server_name _;
    return 301 http://www.dev.mysite.com;
  • Made the changes, but now when I access dev.mysite.com, I get the old webRoot (usr/share/nginx/html) and on accessing localhost, I get the page application stored at /var/www/html! I put in the mymachine at /etc/hostname and in /etc/hosts but the hostname hasnt changed on relog. – user1502178 Oct 13 '14 at 5:40
  • Oh yeah, CentOS. Change the hostname in /etc/hosts and /etc/sysconfig/network (for this last one set it with HOSTNAME=mymachine) and REBOOT. Remove /etc/hostname, that's for debian. For the nginx part, it's not really the place here, ask on serverfault. Check updated answer. – Xavier Lucas Oct 13 '14 at 9:29
  • HOSTNAME is correct there! – user1502178 Oct 13 '14 at 9:35
  • @user1502178 Reboot. – Xavier Lucas Oct 13 '14 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.