2

I am having a problem with the virtual hosts using nginx no matter what I do I get to www/html and not the www/example1 folder. Can anyone please spot the problem? Am I missing something?

pi@homeserver:/etc/nginx/sites-enabled $ ls -l
total 4
-rw-r--r-- 1 root root 467 Sep  4 19:41 default
lrwxrwxrwx 1 root root  38 Sep  4 19:43 example1 -> /etc/nginx/sites-available/example1

default file is

server { 
  listen 80; 

  root /var/www/html; 
  index index.php index.html index.htm; 

  server_name localhost; 

  location / { 
     try_files $uri $uri/ =404; 
  }

    location ~\.php$ { 
        fastcgi_pass unix:/var/run/php5-fpm.sock; 
        fastcgi_split_path_info ^(.+\.php)(/.*)$; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        fastcgi_param HTTPS off; 
        try_files $uri =404;
         include fastcgi_params; 
    }
}

example1 file is

server { 
  listen example1.com:80; 

  root /var/www/example1.com; 
  index index.php index.html index.htm; 

  server_name example1.com www.example1.com; 

  location / { 
     try_files $uri $uri/ =404; 
  }

    location ~\.php$ { 
        fastcgi_pass unix:/var/run/php5-fpm.sock; 
        fastcgi_split_path_info ^(.+\.php)(/.*)$; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        fastcgi_param HTTPS off; 
        try_files $uri =404;
         include fastcgi_params; 
    }
}

i get this error in logs

2016/09/04 21:41:08 [emerg] 1788#0: invalid host in "http://www.example1.com:80" of the "listen" directive in /etc/nginx/sites-enabled/example1.com:2
  • Are config files from sites-enabled directory included in main nginx configuration? Have you checked output of 'nginx -t' command? Have you reloaded nginx after adding new vhost? – StefanR Sep 4 '16 at 18:02
  • config files from sites-enabled are not in main nginx configuration? im not sure where that is and never saw this step in any tutorial could you please tell me where should i link these files to? nginx -t is OK, i realoded and even rebooted and still get the www/html folder – Daniel Tikanti Sep 4 '16 at 18:08
  • Files from that directory may be loaded perhaps in Debian or at least in some linux/unix.. But at least in CentOS and RHEL that is not the case. You should check nginx.conf, which probably is located in /etc/nginx/. And you should tail your nginx logs same time you are accessing the site, you can see a lot from the logs. – StefanR Sep 4 '16 at 18:12
  • this is what I see in access log: 10.0.0.138 - - [04/Sep/2016:21:16:35 +0300] "GET / HTTP/1.1" 200 15 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" 10.0.0.138 - - [04/Sep/2016:21:16:35 +0300] "GET /favicon.ico HTTP/1.1" 404 200 "example1.com" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" – Daniel Tikanti Sep 4 '16 at 18:18
  • Well I cant see anything wrong currently in your configs or in that log... Maybe you should test with some simple files in both www roots. Like file with "<h1>default</h1>" as content in the default site etc... – StefanR Sep 4 '16 at 18:24
2

From the error message, it seems that nginx is having difficulty resolving the statement listen example1.com:80; to an IP address.

Providing an IP address to the listen directive is useful if you have a multihomed server and wish to restrict services to one interface.

In the majority of cases, the statement listen 80; will suffice. I suggest you use the following for both server blocks:

server {
    listen 80;
    ...
}

See this document for more.

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.