0

I have two sites running as below. Server : nginx/1.14.2 / Debian 10

  • primary site mydomain.com in /var/www/mydomain directory
  • sub site blog.mydomain.com in /var/www/blog` directory

See DNS entries.

Type    Name          Content       TTL 
A       blog          34.72.11.9    auto
A       mydomain.com  34.72.11.9    auto
CNAME   www           mydomain.com  auto

Configured nginx virtual host seperately

server_name  mydomain.com www.mydomain.com

and

server_name  sub1.mydomain.com

But, blog.mydomain.com also loading with mydomain.com content.

Any idea ?

2

I can't leave comments, I need four more reputation points but I may be able to help.

With the information you have provided it's difficult to figure out but as a guide I do the following. I use Nginx as a reverse proxy which caches an Apache on another server but the config should be the same.

Before mucking around with your nginx configuration make certain you do a backup. I am also assuming you are on a Linux machine not windows.

In my nginx.conf with the http block;

include /etc/nginx/sites-enabled/*.conf

Each domain has its own config for easier debugging, for example /etx/nginx/sites-enabled/mydomain.com.conf

server {

    listen 192.168.0.100:80;

    server_name mydomain.com www.mydomain.com;
    root /var/www/mydomain.com;
    index index.html index.php;

}

Now for /etc/nginx/sites-enabled/blog.mydomain.com.conf;

server {

        listen 192.168.0.100:80;

        server_name blog.mydomain.com;
        root /var/www/blog.mydomain.com;
        index index.html index.php;

    }

Now tell nginx to test the configuration and then reload.

sudo nginx -t
sudo nginx -s reload

If the above doesn't work make sure there are no other "server" blocks overriding and that "default_server" is not set anywhere. Ensure proper file ownership and access rights and that the nginx user can read the required files.

13
  • Thanks for quick reply. Yes, I am following the same pattern; different sites are kept as different files in /etc/nginx/sites-available/ and symlinked to /etc/nginx/sites-enabled/. server { listen 80; listen [::]:80; root /var/www/blog; index index.php index.html index.htm; server_name blog.mydomain.com;
    – Gineesh
    Apr 20 '20 at 6:24
  • Are there any rewrite rules in your setup that might redirect to mydomain.com? If you are using firefox or google chrome you can right click on page and select inspect. Refresh the page and select the "Network" in the inspector. Is there anything odd about headers & responses? Could it be cached in your browser? Lots of questions but I'll do my best to help. Apr 20 '20 at 6:29
  • Thanks for quick reply. Yes, I am following the same pattern; different sites are kept as different files in /etc/nginx/sites-available/ and symlinked to /etc/nginx/sites-enabled/. default_server is there in default config but not activated. (not in sites-enabled) $ grep -R default_server ./../ ./../sites-available/default: listen 80 default_server; ./../sites-available/default: listen [::]:80 default_server; ./../sites-available/default: # listen 443 ssl default_server; ./../sites-available/default: # listen [::]:443 ssl default_server;
    – Gineesh
    Apr 20 '20 at 6:29
  • 1
    This is my subdomain config server { listen 80; listen [::]:80; root /var/www/blog; index index.php index.html index.htm; server_name blog.mydomain.com; location / { try_files $uri $uri/ =404; #try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; } }
    – Gineesh
    Apr 20 '20 at 6:32
  • 1
    @Yogesch Well spotted on the typing errors and thanks for fixing them. Apr 25 '20 at 22:46

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.