Category Archives: Nginx

Nginx Rewrite Rule to Add Slash to The End of Any URL

Solution 1 If you want to append a slash “/” to the end of any URL, such as: website.com/category => website.com/category/website.com/category => website.com/category/ You can use this Nginx rewrite rule: rewrite ^(.*[^/])$ $1/ permanent;rewrite ^(.*[^/])$ $1/ permanent; Solution 2 If you want to append the slash only if the URL NOT contains a dot, example: […]

Let’s Encrypt on Nginx and SSL Grade A+

A few steps to install Let’s Encrypt on Debian with Nginx and score a A+ grade on SSL Labs. We will install Certbot to simplify the creation and renew of SSL certificates with Let’s Encrypt. Simple tutorial that can help you setup Let’s Encrypt with Nginx in just a few minutes. Login via SSH on […]

Sample Nginx FastCGI Cache Configuration File

Example Nginx FastCGI cache configuration file for a vhost: #FASTCGI MICROCACHE – START# fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=microcache:100m max_size=1024m inactive=1h; add_header X-Cache $upstream_cache_status; #FASTCGI MICROCACHE – END#   server { listen 80; server_name website.com;   […]   #FASTCGI MICROCACHE – START#   set $no_cache 0;   # POST requests and urls with a query string should […]

Rotate Nginx vHosts Log Files with Logrotate

Edit /etc/logrotate.d/nginx and add: /var/www/vhosts/*/logs/*.log { daily missingok rotate 52 compress delaycompress notifempty create 640 nginx adm sharedscripts postrotate [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript }/var/www/vhosts/*/logs/*.log { daily missingok rotate 52 compress delaycompress notifempty create 640 nginx adm sharedscripts postrotate [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript } […]

Block user-agents and referrers with Nginx map

The code below must be included inside the http{} block: ## Add here all user agents that are to be blocked. map $http_user_agent $bad_bot { default 0; libwww-perl 1; ~(?i)(httrack|htmlparser|libwww) 1; }   ## Add here all referrers that are to blocked. map $http_referer $bad_referer { default 0; ~(?i)(babes|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|sex|teen|webcam|zippo|casino|replica) 1; }## Add here all user […]

Compress JavaScript files with Gzip

This is a list of JavaScript content-types: text/js application/javascript application/x-javascript text/javascripttext/js application/javascript application/x-javascript text/javascript Example usage in /etc/nginx/nginx.conf file: gzip on; gzip_http_version 1.1; gzip_comp_level 6; # http://wiki.nginx.org/NginxHttpGzipModule#gzip_disable gzip_disable "msie6"; gzip_proxied any; gzip_types text/plain text/css text/xml text/js application/xml application/javascript application/x-javascript text/javascript application/json application/xml+rss; gzip_buffers 16 8k; gzip_vary on;gzip on; gzip_http_version 1.1; gzip_comp_level 6; # http://wiki.nginx.org/NginxHttpGzipModule#gzip_disable gzip_disable […]

Use Nginx Helper without ngx_cache_purge module

Create a file in your website root folder named purge.php: <?php   // Allow only safe IP addresses //if ( $_SERVER[‘REMOTE_ADDR’] != "SERVER.IP.ADDRESS.HERE" ) die( "IP address not allowed!" );   echo date("Y-m-d H:i:s", time())." :: Checking cache file…<br />";   $cache_path = ‘/var/www/nginx-cache/’;   if ( !is_dir($cache_path) ) die( date("Y-m-d H:i:s", time())." :: ".htmlspecialchars($cache_path)." […]

Nginx PHP file upload size limit

Lets suppose you need to support uploading of 50MB files. Edit /etc/php5/fpm/php.ini file and set these parameters: upload_max_filesize = 50M post_max_size = 50Mupload_max_filesize = 50M post_max_size = 50M Edit /etc/nginx/nginx.conf and set this parameter: http { … client_max_body_size 50m; … }http { … client_max_body_size 50m; … } Restart PHP and Nginx services: service php5-fpm reload […]

Deny access to sensitive files with Nginx

Add this in the vhost file: # Protect specific TXT and config files location ~ /(\.|wp-config.php|readme.html|license.txt|schema.txt|password.txt|passwords.txt) { deny all; }   # Protect ~ files location ~ ~$ { access_log off; log_not_found off; deny all; }   # Protect .git files location ~ /\.git { access_log off; log_not_found off; deny all; }   # Protect […]

1 Billion hits/day with Nginx + PHP5-FPM + MySQL

Recently I tested Nginx + PHP-FPM + MySQL with Blitz.io service: 10,000 concurrent connections with Nginx + PHP-FPM + MySQL I have tested it again, but this time with 20,000 concurrent connections: As you can see from the above image, the rush generated 654,550 successful hits in 60 seconds and the average hit rate of […]