Detailed explanation of the best configuration for Nginx to improve security and performance

Detailed explanation of the best configuration for Nginx to improve security and performance

It mainly shows how to configure X-Frame-Options, X-XSS-Protection, X-Content-Type-Options, Strict-Transport-Security, https and other security configurations in Nginx.

Nginx.conf is configured as follows

# Do not display the Nginx version number on error pages or in the server header server_tokens off;

#Do not allow pages to be displayed from frames or iframes, which can avoid clickjacking
# http://en.wikipedia.org/wiki/Clickjacking
# If you allow [i]frames, you can use SAMEORIGIN or set your allowed urls in ALLOW-FROM
# https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options
add_header X-Frame-Options SAMEORIGIN;

#When your website is user-provided content such as a blog forum, use the X-Content-Type-Options: nosniff header,
# This is to disable content type sniffing in some browsers # https://www.owasp.org/index.php/List_of_useful_HTTP_headers
# Currently supports IE > 8 and above http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx
# http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx
#Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=471020
add_header X-Content-Type-Options nosniff;

# Prevent cross-site scripting (XSS), which is currently supported by most browsers. # It is activated by default. If it is disabled by the user, you can use this configuration to activate it.
# https://www.owasp.org/index.php/List_of_useful_HTTP_headers
add_header X-XSS-Protection "1; mode=block";

#Activate Content Security Policy (CSP), which is supported by most browsers. #Tell the browser that scripts can only be downloaded from this domain and the URLs you explicitly specify.
# http://www.html5rocks.com/en/tutorials/security/content-security-policy/#inline-code-considered-harmful
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://ssl.google-analytics.com https://assets.zendesk.com https://connect.facebook.net; img-src 'self' https://ssl.google-analytics.com https://s-static.ak.facebook.com https://assets.zendesk.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://assets.zendesk.com; font-src 'self' https://themes.googleusercontent.com; frame-src https://assets.zendesk.com https://www.facebook.com https://s-static.ak.facebook.com https://tautt.zendesk.com; object-src 'none'";

server {
listen 443 ssl default deferred;
server_name .forgott.com;

ssl_certificate /etc/nginx/ssl/star_forgott_com.crt;
ssl_certificate_key /etc/nginx/ssl/star_forgott_com.key;

#Enable session re-use to improve https performance# http://vincent.bernat.im/en/blog/2011-ssl-session-reuse-rfc5077.html
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 5m;

# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
ssl_dhparam /etc/nginx/ssl/dhparam.pem;

#Activate server-side protection from BEAST attacks# http://blog.ivanristic.com/2013/09/is-beast-still-a-threat.html
ssl_prefer_server_ciphers on;
# Disable SSLv3 (activated by default since nginx 0.8.19) http://en.wikipedia.org/wiki/Secure_Sockets_Layer#SSL_3.0
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# Choosing Ciphers for Confidentiality and Compatibility # http://blog.ivanristic.com/2013/08/configuring-apache-nginx-and-openssl-for-forward-secrecy.html
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";

# Activate OCSP stapling (mechanism by which a site can convey certificate revocation information to visitors in a privacy-preserving, scalable manner)
# http://blog.mozilla.org/security/2013/07/29/ocsp-stapling-in-firefox/
resolver 8.8.8.8;
ssl_stapling on;
ssl_trusted_certificate /etc/nginx/ssl/star_forgott_com.crt;

# Configure and activate HSTS (HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
#Avoid SSL stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

# ... the rest of your configuration
}

# redirect all http traffic to https
server {
listen 80;
server_name .forgott.com;
return 301 https://$host$request_uri;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to increase your web performance by 3 times by turning on a parameter in Nginx
  • Using X-Sendfile header in Nginx to improve PHP file download performance (for large file downloads)
  • Django uses celery and NGINX to generate static pages to achieve performance optimization
  • Nginx+Tomcat high performance load balancing cluster construction tutorial
  • How to build a high-performance load balancing cluster with Nginx+Tomcat
  • Speed ​​up nginx performance: enable gzip and cache
  • Nginx server configuration performance optimization solution
  • Some suggestions for improving Nginx performance

<<:  Detailed explanation of the method of comparing dates in MySQL

>>:  Explain TypeScript mapped types and better literal type inference

Recommend

How to use ES6 class inheritance to achieve a gorgeous ball effect

Table of contents introduce Implementation steps ...

How to query a record in Mysql in which page of paging

Preface In practice, we may encounter such a prob...

Operate on two columns of data as new columns in sql

As shown below: select a1,a2,a1+a2 a,a1*a2 b,a1*1...

Use of nginx custom variables and built-in predefined variables

Overview Nginx can use variables to simplify conf...

Detailed summary of MySQL and connection-related timeouts

MySQL and connection related timeouts Preface: To...

Solutions to MySql crash and service failure to start

I have been in contact with PHP for so long, but ...

Vue uses WebSocket to simulate the chat function

The effect shows that two browsers simulate each ...

Detailed explanation of upgrading Python and installing pip under Linux

Linux version upgrade: 1. First, confirm that the...

How to switch directories efficiently in Linux

When it comes to switching directories under Linu...

Input file custom button beautification (demo)

I have written such an article before, but I used...

Common considerations for building a Hadoop 3.2.0 cluster

One port changes In version 3.2.0, the namenode p...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

How to solve the error of PyCurl under Linux

Solution to "Could not run curl-config"...