r0g
By:
r0g

nginx multiple websites on same port

July 8, 2017 168 views
Nginx WordPress Debian

Hi,

i want to run two wordpress websites on one nginx server. Here is my config:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

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

    server_name mywebsite1.de www.mywebsite1.de;

    location / {
    try_files $uri/ /index.php?$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/mywebsite1;
    }

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

Second config:

server {
    listen 80;
    listen [::]:80 ipv6only=on;

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

    server_name mywebsite1.de www.mywebsite1.de;

    location / {
        try_files $uri/ /index.php?$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/mywebsite1;
    }

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

While both are enabled via ln -s only one of them is responding. So when i call mywebsite1.de it shows mywebsite1.de but when i call mywebsite2.de it also shows mywebsite1.de. Any ideas?

2 Answers
jtittle MOD July 8, 2017
Accepted Answer

@r0g

Anytime new configuration changes are made or added, you'll need to restart or reload NGINX.

service nginx restart

or

service nginx reload

As a general note, the ipv6only=on directive can be removed from both of the server blocks -- it's on by default :-). When you restart NGINX, it will most likely fail if both have it set.

Beyond that one change, everything else looks okay and should work as expected the symlink isn't working and showing up in sites-enabled.

You can also test configuration using:

nginx -t

...

https://nginx.org/en/docs/http/ngx_http_core_module.html

"this parameter (0.7.42) determines (via the IPV6_V6ONLY socket option) whether an IPv6 socket listening on a wildcard address [::] will accept only IPv6 connections or both IPv6 and IPv4 connections. This parameter is turned on by default. It can only be set once on start."

Thank you! Without ipv6only=on its working

Have another answer? Share your knowledge.