4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

The config looks great. However, a few points:

  1. 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;
    
  2. 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
    
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Oh, thanks. Point #2 is a great advice. Hope to get more of the same, as for #1, that was intentional, since I need to for staging/developing ex. dev-stg.my-site.com which points to another folder, I had to be explicit. Thanks, add more if you don't mind :) \$\endgroup\$ – robue-a7119895 Aug 28 '14 at 16:23
  • 1
    \$\begingroup\$ Ah totally understood on point 1. As for point 2 - another great use of that method is that if you ever need to disable a site for whatever reason, you just delete the symlink and reload nginx. Your original config in sites-available will remain untouched if you ever need to reenable in the future (by creating the symlink again). \$\endgroup\$ – jsanc623 Aug 28 '14 at 16:27

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.