gaggina
gaggina

Reputation: 5425

Nginx and subdomains

I'm trying to getting started with nginx. But I cant really understand what's going wrong with this code.

As you can see there are 2 domains:

  1. mauromarano.it
  2. dev.mauromarano.it

The first domains hosts a wordpress blog.

#####################
# mauromarano.it/  #
####################

server {
    listen 80;
#   listen [::]:80 default_server;

    root /usr/share/nginx/mauromarano.it/public_html;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name mauromarano.it www.mauromarano.it;

    access_log /usr/share/nginx/mauromarano.it/logs/access.log;
    error_log /usr/share/nginx/mauromarano.it/logs/error.log;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location /blog {
            try_files $uri $uri/ /blog/index.php?$args;
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(/blog)(/.*)$;
        fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/mauromarano.it/public_html$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}



#####################
# mauromarano.com   #
####################

server {
    listen 80;
#   listen [::]:80 default_server;

    root /usr/share/nginx/mauromarano.com/public_html;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name mauromarano.com www.mauromarano.com;

    access_log /usr/share/nginx/mauromarano.com/logs/access.log;
    error_log /usr/share/nginx/mauromarano.com/logs/error.log;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;

        fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/mauromarano.com/public_html$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

#####################
# dev.mauromarano.it/  #
####################

server {
    listen 80;
#   listen [::]:80 default_server;

    root /usr/share/nginx/dev.mauromarano.it/public_html;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name dev.mauromarano.it www.dev.mauromarano.it;

    access_log /usr/share/nginx/dev.mauromarano.it/logs/access.log;
    error_log /usr/share/nginx/dev.mauromarano.it/logs/error.log;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }


    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(/blog)(/.*)$;
        fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/dev.mauromarano.it/public_html$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

Where I'm wrong?

My goal Is to having this two domains working. But in this way, the subdomains (dev.mauromarano.it) is not working.

Upvotes: 2

Views: 779

Answers (1)

Sébastien Renauld
Sébastien Renauld

Reputation: 19662

The following clause is not needed for server blocks:

listen 80;
listen [::]:80 default_server;

This should solve the subdomain issue. In addition to this, you are missing the rewrite rules for wordpress. They should be as follows:

location / {
   try_files $uri $uri/ @wordpress;
}
location @wordpress {
   rewrite ^/(.*)$ /index.php?/$1 last;
}

Hopefully this will clear your problem. More info on it would mean more pointed solutions, however, so feel free to comment with any bugs you still have.

Upvotes: 1

Related Questions