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

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

Where is the project location deployed by IntelliJ IDEA using Tomcat?

After IntelliJ IDEA deploys a Javaweb project usi...

How to read the regional information of IP using Nginx and GeoIP module

Install GeoIP on Linux yum install nginx-module-g...

Implementation example of scan code payment in vue project (with demo)

Table of contents Demand background Thought Analy...

Several methods of deploying multiple front-end projects with nginx

I have summarized 3 methods to deploy multiple fr...

JavaScript anti-shake case study

principle The principle of anti-shake is: you can...

Solution to no Chinese input method in Ubuntu

There is no solution for Chinese input method und...

Docker image creation Dockerfile and commit operations

Build the image There are two main ways to build ...

Detailed explanation of Vue advanced construction properties

Table of contents 1. Directive custom directive 2...

CSS overflow-wrap new property value anywhere usage

1. First, understand the overflow-wrap attribute ...

How to import CSS styles into HTML external style sheets

The link-in style is to put all the styles in one...

Nginx forwarding based on URL parameters

Use scenarios: The jump path needs to be dynamica...

Three common methods for HTML pages to automatically jump after 3 seconds

In practice, we often encounter a problem: how to...

Detailed explanation of a method to rename procedure in MYSQL

Recently I have used the function of renaming sto...