Nginx serving wrong site

5,607

The file beryjuorg-blog.conf is missing a server_name directive, which means that server block will be a "catch-all" for requests with no HTTP/1.1 Host header (the domain name) or an undefined Host header. See How nginx processes requests and server blocks for more on this.

Since you have a static IP address, you can make the configuration more explicit by adding the IP address to the listen directive, e.g.

listen 192.168.0.3:80;

The listen directive is not meant to accept a hostname; see the documentation.

Specifying both server_name and an optional IP address for listen should resolve the issue.

Share:
5,607

Related videos on Youtube

BeryJu
Author by

BeryJu

Updated on September 18, 2022

Comments

  • BeryJu
    BeryJu over 1 year

    I have nginx setup on my vps with a static ip and 2 domains (beryju.org and nope.berlin).

    I want to serve:

    • Site 1 to beryju.org
    • Site 2 to blog.beryju.org
    • Site 3 to i.beryju.org
    • Site 4 to nope.berlin

    Additionally I want beryju.org and blog.beryju.org to support SSL.

    But with my current configuration, I am getting:

    • Site 1 not at all
    • Site 2 on beryju.org and blog.beryju.org
    • Site 3 on i.beryju.org
    • Site 4 on nope.berlin

    I have tried and it also doesn't works when I remove the hostname from the listen statement, i.e.

    listen i.beryju.org:80;
    

    to

    listen 80;
    

    in every file.

    beryjuorg-blog.conf

    server {
        listen blog.beryju.org:80;
         listen blog.beryju.org:443 ssl;
        ssl_certificate /home/beryju/SSL/nginx/beryju.org.cert;
        ssl_certificate_key /home/beryju/SSL/nginx/beryju.org.key;
    
        access_log /var/log/nginx/beryjuorg-blog.access.log;
        error_log /var/log/nginx/beryjuorg-blog.error.log;
    
        root /home/beryju/Apps/Ghost/;
        index index.html index.htm index.php;
    
        location  / {
            proxy_pass http://127.0.0.1:2368;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
    }
    

    beryjuorg-image-hoster.conf

    server {
        listen i.beryju.org:80;
    
        server_name i.beryju.org;
    
        access_log /var/log/nginx/beryjuorg-image-hoster.access.log;
        error_log /var/log/nginx/beryjuorg-image-hoster.error.log;
    
        root /usr/share/nginx/i.beryju.org;
        index index.html index.htm index.php;
    
        location  /gyazo.php {
            try_files $uri $uri/ =404;
            fastcgi_index index.php;
            include fcgi.conf;
            fastcgi_pass unix:/var/run/php-fcgi-beryjuorg-image-hoster-php-fcgi-0.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
    
        location  / {
            try_files $uri $uri/ =404;
        }
    
    }
    

    beryjuorg.conf

    server {
        listen beryju.org:80;
        listen beryju.org:443 ssl;
        ssl_certificate /home/beryju/SSL/nginx/beryju.org.cert;
        ssl_certificate_key /home/beryju/SSL/nginx/beryju.org.key;
        server_name beryju.org;
    
        access_log /var/log/nginx/beryjuorg.access.log;
        error_log /var/log/nginx/beryjuorg.error.log;
    
        root /usr/share/nginx/beryju.org/;
        index index.html index.htm index.php;
    
        location ~ [^/]\.php(/|$) {
            fastcgi_index index.php;
            include fcgi.conf;
            fastcgi_pass unix:/var/run/php-fcgi-beryjuorg-php-fcgi-0.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
    }
    

    nopeberlin.conf

    server {
        listen nope.berlin:80;
    
        server_name nope.berlin;
    
        access_log /var/log/nginx/nopeberlin.access.log;
        error_log /var/log/nginx/nopeberlin.error.log;
    
        root /usr/share/nginx/nope.berlin/;
        index index.html index.htm index.php;
    
        location  / {
            autoindex on;
        }
    
    }
    
  • BeryJu
    BeryJu almost 10 years
    Thank you very much; I don't know how I could miss that while trying to fix this.
  • dartonw
    dartonw almost 10 years
    @BeryJu, you're quite welcome. I've been in that situation to the point of frustration, and sometimes all it takes to resolve is a fresh pair of eyes.
  • Mohit Gupta
    Mohit Gupta over 4 years
    The listen directive is not meant to accept a hostname; see the documentation. This is incorrect, see: Both address and port, or only address or only port can be specified. An address may also be a hostname, for example: listen 8000; listen *:8000; listen localhost:8000;