Category Archives: Nginx

Redirect 404 Errors to Homepage with Nginx

You can redirect 404 errors to homepage with Nginx. To do so, just add this inside server {}: # Redirect 404 errors to homepage error_page 404 = @myownredirect; # My custom 302 redirect to homepage location @myownredirect { return 302 /; }# Redirect 404 errors to homepage error_page 404 = @myownredirect; # My custom 302 […]

Nginx HTTP to HTTPS and non-www to www behind Google Load Balancer

First create two forwarding rules on Google Load Balancer (frontend): – One for HTTPS (port 443) traffic – One for HTTP (port 80) traffic Then make sure both rules use the same static IP address. Now on the Compute Engine backend, edit the Nginx config file: server { listen 80; server_name localhost; access_log /var/www/nginx/logs/access.log main; […]

Restarting nginx: [emerg]: directive “rewrite” is not terminated by “;”

If you get this erro message when restarting Nginx: Restarting nginx: [emerg]: directive "rewrite" is not terminated by ";"Restarting nginx: [emerg]: directive "rewrite" is not terminated by ";" Make sure to read the docs for http://wiki.nginx.org/HttpRewriteModule#rewrite Note: for curly braces( { and } ), as they are used both in regexes and for block control, […]

Content-Security-Policy-Report-Only

This is a quick post about Content-Security-Policy-Report-Only: Content-Security-Policy-Report-Only: default-src https: data: blob: ‘unsafe-inline’ ‘unsafe-eval’; report-uri /_cspContent-Security-Policy-Report-Only: default-src https: data: blob: ‘unsafe-inline’ ‘unsafe-eval’; report-uri /_csp Taken from HTTP headers of https://vimeo.com To implement it with Nginx use add_header like this: add_header Content-Security-Policy-Report-Only "default-src https: data: blob: ‘unsafe-inline’ ‘unsafe-eval’; report-uri /_csp";add_header Content-Security-Policy-Report-Only "default-src https: data: blob: ‘unsafe-inline’ […]

Rotate Nginx vHosts Log Files Every 1 Year

Set logrotate to keep 1 year (365 days) of log files: sed -i ‘s/rotate 52/rotate 365/’ /etc/logrotate.d/nginxsed -i ‘s/rotate 52/rotate 365/’ /etc/logrotate.d/nginx

Nginx Load Balancing Example

Nginx can be used as load balancer, so it can split the traffic within N servers specified in the upstream section of the Nginx configuration file. This is useful to scale the web traffic if it becomes too much for a single server or VPS. Using Nginx as load balancer is incredibly easy, in short, […]

Nginx Static Content Test (Concurrent Connections)

Here I test how many concurrent connections Nginx can handle when serving static content. In this test I benchmark Nginx on port 80 (HTTP) and on port 443 (HTTPS). The static file is a simple .HTML file and this is the Nginx.conf file I used in the test: user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; […]

Rate Limit Requests per URL with Nginx

You can rate limit requests per URL with Nginx. So lets say you want to limit requests on /v2/api URL to max 150 per minute independently from the remote IP address, you can do like this: Add this inside http{} block: limit_req_zone $server_name zone=one:100m rate=150r/m;limit_req_zone $server_name zone=one:100m rate=150r/m; Then inside the server{} block add this: […]

Rate Limit Requests per IP with Nginx

You can limit requests per IP per second with Nginx. Add this line inside http{} block to limti 1 request per second per IP: limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; Lets say you want to limit requests only on /events/: Add this text inside server{} block: location /events/ { limit_req zone=one nodelay; }location /events/ […]

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: […]