HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Setting .conf files for Nginx

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
confnginxsettingfilesfor

Problem

I use Nginx as a server, and I currently use this for configuration of each my sites. Basically, I have multiple files like this located in /etc/nginx/conf.d/.

#example.conf
server {
  listen             80;
  server_name        www.my-site.com  my-site.com;
  root               /var/www/html/my-site.com;

  location / {
         rewrite ^/category/(.*)$ /category.php?id=$1 last;
         rewrite ^/profile/(.*)$ /profile.php?id=$1 last;
         try_files $uri $uri/ /index.php?$query_string;
  }

  index  index.html index.htm index.php;

  error_page  404              /404.html;
        location = /var/www/html/nginx/error/404.html {
        root       /var/www/html/nginx/error/;
  }

  error_page   500 502 503 504  /50x.html;
       location = /var/www/html/nginx/error/50x.html {
       root        /var/www/html/nginx/error/error/;
  }

  location ~ \.php$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
  }
}


So, I have 6 sites with their own file like this, and since I am about to do a complete re-install of my server, I would like to know how I can improve this configuration, both security and optimization wise.

Solution

The config looks great. However, a few points:

-
If your server responds to both the www and non-www version of the site, just leave a wildcard. So server name should read:

server_name    *.my-site.com;


-
You shouldn't put your configs in /etc/nginx/conf.d. Instead, place them in /etc/nginx/sites-available and then symlink them (ln -s) to /etc/nginx/sites-enabled. So, your /etc/nginx directory should look like:

/etc/nginx/
     nginx.conf
     sites-available/
     my-site.conf
     my-other-site.conf
     yet-another-site.conf
 sites-enabled/
     my-site.conf -> /etc/nginx/sites-available/my-site.conf
     my-other-site.conf -> /etc/nginx/sites-available/my-other-site.conf
     yet-another-site.conf -> /etc/nginx/sites-available/yet-another-site.conf

Code Snippets

server_name    *.my-site.com;
/etc/nginx/
     nginx.conf
     sites-available/
     my-site.conf
     my-other-site.conf
     yet-another-site.conf
 sites-enabled/
     my-site.conf -> /etc/nginx/sites-available/my-site.conf
     my-other-site.conf -> /etc/nginx/sites-available/my-other-site.conf
     yet-another-site.conf -> /etc/nginx/sites-available/yet-another-site.conf

Context

StackExchange Code Review Q#61366, answer score: 4

Revisions (0)

No revisions yet.