Before because of

Many Nginx deployment and configuration articles have been published online, including two that I have privately published:

  • Prerequisites Nginx configuration at the back end
  • Prerequisite front-end Nginx configuration

Sort out for the time is needed, copy and paste can be used.

In real life, however, you will definitely need to add or remove Nginx configurations. You have to search the Internet, copy and paste the bug and adjust it…

We’ll have to save it for later. More difficult to find also have to tidy up… It was a real problem

The consequences

Today I would like to recommend a “Nginx configuration tool “, which can be used to generate common configurations with one click of the configuration variables. Say goodbye to cumbersome and inefficient configurations 👋

  • Nginxconfig Online configuration website
  • Nginxconfig making project

Nginxconfig currently supports:

  • Angular, React, Vue, node.js
  • PHP, Python,
  • WordPress, Magento, Drupal
  • Cache, Https, log, etc.

use

Realize the *. Myweb.com domain name automatically redirect to myweb.com configuration, and enable the HTTP force redirect to HTTPS configuration.

Installation steps

/etc/nginx/sites-available/myweb.com.conf

server {
	listen 443 ssl http2;
	listen[: :] :443 ssl http2;

	server_name myweb.com;
	root /var/www/myweb.com/public;

	# SSL
	ssl_certificate /etc/letsencrypt/live/myweb.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/myweb.com/privkey.pem;
	ssl_trusted_certificate /etc/letsencrypt/live/myweb.com/chain.pem;

	# security
	include nginxconfig.io/security.conf;

	# index.html fallback
	location / {
		try_files $uri $uri/ /index.html;
	}

	# additional config
	include nginxconfig.io/general.conf;
}

# subdomains redirect
server {
	listen 443 ssl http2;
	listen[: :] :443 ssl http2;

	server_name *.myweb.com;

	# SSL
	ssl_certificate /etc/letsencrypt/live/myweb.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/myweb.com/privkey.pem;
	ssl_trusted_certificate /etc/letsencrypt/live/myweb.com/chain.pem;

	return 301 https://myweb.com$request_uri;
}

# HTTP redirect
server {
	listen 80;
	listen[: :] :80;

	server_name .myweb.com;

	include nginxconfig.io/letsencrypt.conf;

	location / {
		return 301 https://myweb.com$request_uri; }}Copy the code

/etc/nginx/nginx.conf

# Generated by nginxconfig.io
# https://nginxconfig.io/? 0.domain=myweb.com&0.php=false&0.index=index.html&0.fallback_html

user www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;

events {
	multi_accept on;
	worker_connections 65535;
}

http {
	charset utf-8;
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	server_tokens off;
	log_not_found off;
	types_hash_max_size 2048;
	client_max_body_size 16M;

	# MIME
	include mime.types;
	default_type application/octet-stream;

	# logging
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log warn;

	# SSL
	ssl_session_timeout 1d;
	ssl_session_cache shared:SSL:10m;
	ssl_session_tickets off;

	# Diffie-Hellman parameter for DHE ciphersuites
	ssl_dhparam /etc/nginx/dhparam.pem;

	# Mozilla Intermediate configuration
	ssl_protocols TLSv1.2 TLSv1.3;
	ssl_ciphersECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDH E-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;# OCSP Stapling
	ssl_stapling on;
	ssl_stapling_verify on;
	resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
	resolver_timeout 2s;

	# load configs
	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}
Copy the code

/etc/nginx/nginxconfig.io/security.conf

# security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

# . files
location ~ / \. (? ! well-known) {
	deny all;
}
Copy the code


expand

That’s enough for everyday development. If you can’t resist and want to show off your high-end operation. You can participate in the development of the project itself; The NginXConfig project itself is an MIT open source protocol, and you can iterate on it to create your own version