Dec 4, 2015

0 Nginx snippets


Nginx configuration code snippets.



Basic commands


nginx -t  - test configuration file.
nginx -s reload - reload Nginx configuration file, without webserver restart.

Force Trailing Slash

Add a slash "/" at end of every URL.
rewrite ^([^.\?]*[^/])$ $1/ permanent;

Redirect to page


server {
    location = /oldpage.html {
    return 301 http://example.org/newpage.html;
    }
}

Redirect to domain


server {
    server_name old-site.com
    return 301 $scheme://new-site.com$request_uri;
}

Redirect to specific URL


location /old-site {
    rewrite ^/old-site/(.*) http://example.org/new-site/$1 permanent;
}

Forbid browsers to cache content


location = /empty.gif {
    empty_gif;
    expires -1;
}

Gzip compression


gzip  on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
    gzip_disable  "msie6";

SSL cache


ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m; 

Forbid Iframe


add_header X-Frame-Options SAMEORIGIN;

Forbid security scanners


if ( $http_user_agent ~* (nmap|nikto|wikto|sf|sqlmap|havij|appscan) ) {
    return 403;
}

Accept 80 and 443 SSL


server {
  listen 80;
  listen  443 ssl;                 # This SSL switch is important!!!.
  server_name servername;

0 comments :

Post a Comment

Comment: