Question

nginx multiple websites on same port

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?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Jonathan Tittle
DigitalOcean Employee
DigitalOcean Employee badge
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.”

I have same issue with a primary and sub-domain. Sub-domain always loads the main domain’s content. Static html file loads (index.html) correctly - the problem is with fastcgi configuration. Any more suggestions on fastcgi conf?

  • no errors in conf
  • ipv6only=on not given

Thank you! Without ipv6only=on its working

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.