V
V
vkrzt2014-09-11 00:06:44
Nginx
vkrzt, 2014-09-11 00:06:44

How to properly set up a subdomain in nginx?

Good afternoon,
Finally, I decided to switch from the good old Apache to the new and progressive nginx.
I set up php-fpm in conjunction, installed all the necessary modules, and it seemed to me that everything seemed more transparent and easier than with Apache, I ran a couple of scripts on the default config and was pleased with the result.

And now it was the turn of the host configuration, and then I ran into a problem:

there is www.site1.com and there is a subdomain.site1.com

There are 2 configs in sites-enabled, for www.site1.com:

server {
        listen   80;
        root /usr/share/nginx/www/site1;
        index index.php index.html index.htm;
        server_name site1.com *.site1.com www.site1.com;
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www/site1.;
        }
        location ~ \.php$ {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

                 }
}


for subdomain.site1.com

server {
        listen   80;
        root /usr/share/nginx/www/subdomain.site1;
        index index.php index.html index.htm;
        server_name subdomain.site1.com;
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www/subdomain.site1;
        }
        location ~ \.php$ {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                 }
}


With this configuration, when I go to site1.com, I get the contents of the subdomain.site1.com folder .

Where am I making a mistake,

Thanks for the answers.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
coder4web, 2014-09-11
@kinozol

Your config should work, but fix the error, there is an extra dot at the end of the path.
root /usr/share/nginx/www/site1.;

I
Ivan Kiselev, 2014-09-11
@homelessness

The nginx config looks quite acceptable, to avoid collisions, you can stuff everything into one virtual host and put the server with the subdomain in front of the server with the domain.
Maybe it's php-fpm ? I see you are sending everything to one php-fpm socket. Try splitting the domain and subdomain pool into different php-fpm configs, specifying different sockets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question