Nginx Configuration for Multiple Domains

When hosting multiple sites on a single server using Nginx, it's essential to ensure that each site's configuration is correct to avoid errors like 403 Forbidden.

Example Configuration

Here’s an example configuration for one of the domains:

server {
    server_name www.mysite2.name;
    return 301 $scheme://mysite2.name$request_uri;
}

server {
    server_name mysite2.name;

    root /usr/share/nginx/mysite2.name/live/;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.html index.php;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Troubleshooting 403 Errors

The error message [error] 13108#0: *1 directory index of "/usr/share/nginx/mysite2.name/live/" is forbidden indicates that Nginx is unable to access the specified directory. Here are some common reasons:

  • Permissions: Ensure that the Nginx user has read permissions for the directory and its parent directories.
  • Directory Index: If there is no index.html or index.php file in the specified directory, Nginx will return a 403 error. Make sure that the index files are present.
  • Symlink Issues: If live is a symlink, ensure that the target directory has the correct permissions and that Nginx is configured to follow symlinks.

Conclusion

By ensuring proper permissions and configurations, you can successfully host multiple domains on a single Nginx server without encountering 403 errors.