Optimizing nginx site configuration

Ivan V. asked:

First time working with nginx.
Due to my limited knowledge of location directive precedence I had to repeat the code
at two places (fastcgi).
Can I optimize this file and not repeat code?

This is my server file.

server{
  server_name www.theme-dev.local;
  return 301 $scheme://theme-dev.local$request_uri;
}

server {
listen 80;

root /var/www;

error_log /var/log/nginx/theme-dev.local/server.log warn;

index index.php index.html;

server_name theme-dev.local;

chunked_transfer_encoding off;
proxy_buffering off;

location = /robots.txt {
    access_log off;
    log_not_found off;
}

location = /favicon.ico {
    access_log off;
    log_not_found off;
}


location ~ /.ht {
    access_log off;
    deny all;
 }


location ~* /themes/subfolder/wordpress_home/(wp-admin|wp-login.php)$ {
    auth_basic "WordPress Login";
    auth_basic_user_file /etc/nginx/my_auth/.htpasswd;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    fastcgi_index index.php;
    include fastcgi_params;

}

location /themes/subfolder/wordpress_home/ {
    try_files $uri $uri/ /themes/subfolder/wordpress_home/index.php?q=$uri&$args;
}

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

}

My answer:


That is what include is for. I can see that you’ve already used it twice!


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.