Skip to content

Instantly share code, notes, and snippets.

Last active August 8, 2023 08:39
Star You must be signed in to star a gist
Save konklone/6532544 to your computer and use it in GitHub Desktop.
nginx TLS / SSL configuration options for
# Basically the nginx configuration I use at
# I check it using
# To provide feedback, please tweet at @konklone or email
# Comments on gists don't notify the author.
# Thanks to WubTheCaptain ( for his help and ciphersuites.
# Thanks to Ilya Grigorik ( for constant inspiration.
server {
listen 80;
return 301 https://$host$request_uri;
server {
# 'http2' requires nginx 1.9.5+. If using older nginx, replace with 'spdy'.
listen 443 ssl http2;
# Path to certificate and intermediates, *omitting* the root.
ssl_certificate /path/to/;
# Path to private key used to create certificate.
ssl_certificate_key /path/to/;
# HTTP Strict Transport Security: tells browsers to require https:// without first checking
# the http:// version for a redirect. Warning: it is difficult to change your mind.
# max-age: length of requirement in seconds (31536000 = 1 year)
# includeSubdomains: force TLS for *ALL* subdomains (remove if this is not what you want)
# preload: indicates you want browsers to ship with HSTS preloaded for your domain.
# Submit your domain for preloading in browsers at:
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
# If you won't/can't turn on HTTPS for *all* subdomains, use this simpler version:
# add_header Strict-Transport-Security 'max-age=31536000';
ssl_prefer_server_ciphers on;
# This requires strong forward secrecy (ECDHE) for all connections.
# However, it blocks IE8+XP and Android 2.3.
# Uncomment to require strong forward secrecy (ECDHE) in most clients, with a
# non-FS exception (DES-CBC3-SHA) for IE8/XP, and plain DHE for Android 2.3 users.
# Allows all modern and legacy clients to connect over TLS.
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
# Uncomment for only the latest TLS, if you can drop IE8-IE10 and Android 4.3.
# ssl_protocols TLSv1.2;
# Turn on session resumption, using a 10 min cache shared across nginx processes,
# as recommended by
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
keepalive_timeout 70;
# OCSP stapling: nginx will poll the CA for signed OCSP responses, and
# send them to clients so clients don't make their own OCSP calls.
# The ssl_trusted_certificate is a chain of intermediates *including* the
# root certificate, and *excluding* the cert for your domain.
# See
ssl_stapling on;
ssl_stapling_verify on;
resolver valid=86400;
resolver_timeout 10;
ssl_trusted_certificate /path/to/;
Copy link

So I can't get OCSP stapling for working properly on, however it does respond correctly to openssl s_client connect -CAfile /path/to/cafile.crt -tls1 -tlsextdebug -status. Some observations, my ssl_trusted_certificate file is of the form:


Also, if you are hosting more than one ssl site on nginx, there needs to be one set as the default_server and that needs to have stapling enabled. Otherwise none of the other ssl sites willl have stapling enabled, regardless of configuration.

Copy link

kcivey commented Oct 16, 2014

Thanks. I finally did some of this and plan to do most of the rest soon.

Copy link

For a strategy to use this with SNI-hosts, head on over to my fork

Copy link

Is it worth adding tcp_nodelay for nginx 1.7.8+ (see entry 6)? I have no idea.

Copy link

How did you select your ciphersuite? Here's the cargo-cult: lists three with a rationale for each:

Includes RC4 (because it resists to BEAST):

Excludes RC4 (because it has weaknesses):

Uses RC4 as last resort (e.g. for old TLS versions):

All three are cargo-culted to and the last one is at has three configurations with explanation. has one configuration with explanation.

Copy link

@jpmckinney I don't remember where I get my first draft of them, but they've evolved over time as I've read different things, and as people wrote to me directly with suggestions (wubthecaptain in particular was super helpful.). So it's no longer something I've just copy-pasted from a blog post.

The set I have now ensures strong forward secrecy on everything but IE8+XP, and for that there's a working non-RC4 carveout, in the form of 3DES:

Some of this is cargo-culted back to Qualys, I'm sure -- I don't know why 3DES is stronger than RC4 offhand -- but I'm comfortable with the empirical evidence that various clients select different ciphers that offer different guarantees. My priorities are: forward secrecy as much as possible, avoid RC4 as much as possible. I think I've got that.

Copy link

Cargo-cult isn't necessarily bad - actually, in this case, it makes it harder to evaluate whether your ciphersuite is better or worse than others' :) The sslmate mkconfig recommends the Mozilla "modern" profile, though that cuts out a lot of old browsers that many people still use. I'm using for now.

Copy link

The apparent 'default_server' requirement in nginx is looking more and more like a bug to me. I have a self-signed cert on a default_server SSL server. This is 100% intentional to disable all non-SNI clients. The certificate itself is carefully crafted to let the user know that their web browser is broken and that they need to upgrade to something reasonable and modern. It is also signed for zero domains. Obviously, there is no OCSP stapling server for this cert and there is no content for the website either should the user choose to "add an exception" and/or "proceed anyway". The only option for the user is to upgrade, which is precisely what should happen anyway because the outdated browsers being excluded are insecure and need to stop being used.

However, nginx seems to require the 'default_server' to have OCSP stapling enabled. At best, it's a broken dependency requirement. That is, I have to set up an OCSP server to host my only self-signed cert (and probably regenerate the cert too) to get nginx to do OCSP stapling for the other 'server' entries. Really? That's a waste of web server system resources to "fix" this problem and makes OCSP stapling more trouble than it is worth. But it's more likely a bug in the nginx SSL module itself where this very scenario wasn't conceived of. However, intentionally disabling non-SNI clients should be the defacto standard approach to SSL and a server operator should be able to just set stapling directives on the exact 'server' entries that they want it enabled on. The only valid conclusion that can be drawn is that OCSP stapling support is half-baked, barely functional tech in nginx.

Copy link

Pilvinen commented Oct 9, 2015

I followed your instructions and copied the above nginx.conf and replaced the one in /etc/nginx/.
Then I reloaded with "service nginx restart"
And ran: "nginx -t" and then I get this:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:10
nginx: configuration file /etc/nginx/nginx.conf test failed

And no https for me. What now?

Copy link

@Pilvinen That's an issue somewhere in your nginx.conf file. The above should be included inside a server block somewhere, included dynamically. See for an example.

Copy link

laike9m commented Oct 24, 2015

Buffer size of 1400 bytes fits in one MTU.

I haven't found any documentation saying this is the best practice.

Here's what's in Nginx's doc:

By default, the buffer size is 16k, which corresponds to minimal overhead when sending big responses. To minimize Time To First Byte it may be beneficial to use smaller values, for example:
ssl_buffer_size 4k;

Did you do some test before setting it to 1400?

Copy link

@laike9m I just blindly copied it from @igrigorik's config for, as I blindly trust him on all things regarding TLS performance. =)

Copy link

laike9m commented Oct 26, 2015

I see. Tests on my website shows setting buffer size from 16k(default) to 1400 reduced TTFB, but increased page download time.

Copy link

1492 is an ethernet jumbo frame size

Copy link

JanKanis commented Jan 7, 2016

@cubiclesoft or others who have the same problem: The ssl_stapling not working if it is not enabled in the default server seems to be this bug: The actual bug is in openssl, fixed with OpenSSL 1.0.0m/1.0.1g/1.0.2, so check your openssl version (nginx -V)

Copy link

@konklone you're scoring an F at ssllabs.

 OpenSSL Padding Oracle vuln.
(CVE-2016-2107) Yes  INSECURE (more info). 

Copy link

Peneheals commented Nov 2, 2016

typo at resolver section.

it has to be resolver valid=86400s; according to the nginx manual.

and DES-CBC3-SHA is affected by BEAST, so I suggest disabling here.

Copy link

C0nw0nk commented Sep 24, 2018

Should use a privacy respecting and worlds fastest DNS resolver instead of Google's and IPv4 resolver what is slow and Does not respect privacy or security. Cloudflares resolver also includes both IPv4 IPv6 resolver and alternative fallback DNS resolver alternative resolver for is for IPV4 is and IPv6 2606:4700:4700::1001 as can be found on their website privacy security and speed comes first.

Here is my code for their resolver to support both IPv4 and IPv6 (Can disable IPv6 and will still work universal setup)

#Cloudflare resolver
resolver [2606:4700:4700::1111] [2606:4700:4700::1001];

Fastest DNS resolvers in the world ( is the fastest most secured and private unlike google)!dns-resolvers

Copy link

Fantastic SSL config and discussion here, thanks guys... we used some of your suggestions in our SlickStack settings:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment