My Nginx configurations for Virtual Host, SSL etc. – Nivesh's blog
Published on

My Nginx configurations for Virtual Host, SSL etc.

Authors
Table of contents

Here are my configurations I use for my Laravel apps. Hope someone find these useful.

Virtual Host

# /etc/nginx/sites-available/{{domain}}.conf
server {
    listen 80;
    listen [::]:80;
    server_name www.{{domain}} {{domain}};
    include snippets/letsencrypt.conf;
    return 301 https://{{domain}}$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.{{domain}};
    ssl_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/{{domain}}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    include snippets/ssl-params.conf;
    return 301 https://{{domain}}$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    index index.php;

    server_name {{domain}};
    root {{deploy_path}}/current/public;

    client_max_body_size 100m;

    access_log "/var/log/nginx/{{application}}nginx_access.log";
    error_log "/var/log/nginx/{{application}}nginx_error.log";


    # check file exist and send request sting to index.php
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # allow execute all php files
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    # disallow access to apache configs
    location ~ /\.ht {
        deny all;
    }

    # disallow access to git configs path
    location ~ /\.git {
        deny all;
    }

    ssl_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/{{domain}}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{{domain}}/fullchain.pem;
    include snippets/ssl-params.conf;
}

# sudo ln -s /etc/nginx/sites-available/{{domain}}.conf /etc/nginx/sites-enabled/{{domain}}.conf

Let's encrypt

# /etc/nginx/snippets/ssl-params.conf
location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root /var/www/letsencrypt;
}
# /etc/nginx/snippets/ssl-params.conf
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
#ssl_session_tickets off; # Requires nginx >= 1.5.9

ssl_stapling on; # Requires nginx >= 1.3.7
ssl_stapling_verify on; # Requires nginx => 1.3.7

resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
#add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

P.S. Suggestions/changes are welcome

I've got more coming...

Want to hear from me when I add new post? Sign up for my newsletter and I'll email you every time I post a new article. (I'll never share your email and only send you updates about new articles.)