xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

NGINX configure auto generator

NGINX configure auto generator

The easiest way to configure a performant, secure, and stable NGINX server.

https://www.digitalocean.com/community/tools/nginx

demo

https://www.digitalocean.com/community/tools/nginx?domains.0.server.domain=xgqfrms.xyz&global.https.ocspCloudflareType=both&global.https.ocspGoogleType=both&global.https.ocspOpenDnsType=both

# Download the generated config: nginxconfig.io-xgqfrms.xyz.tar.gz
# and upload it to your server's/etc/nginx directory.

# or, Copy a base64 string of the compressed config, paste it in your server's command line and execute it.

$ echo 'H4sIAHNbUF8CA+0aaXPbuNWf9StQb6bZpMtbhyuNx+OxvbFn7NgTeTvb2bgciAQprEGAC4A6UmV/ex8PSaSOJO007hHD5qF3AQ94eHjAI48pn5mB4NHBVys2lF6vVzyhbD69Trt34Hg9t213Op2ud1CS28g+eIKSKY0lQgffaPkOvSGcSKxJiEZzxHNzyK2BxiYVre/QWOtU9S1rOp2aIY2pxkwEBHMwmcSCK8k41XNLC8GUVXCfhCLBlCvTNhWREyLNEnA8i3+LZKLM2fzDH2MmRpiZhXRTBCo9YyILI4YluZ+n5Hgk9Hib6I0QMfsUwW1K+DlXK4pWK4M2oM0Cyhgh1njQSmm4hUWWzHipiwn4QWsq5CORfipBc6WIKohwpsUKJRlNqPa5iCgjqNvpeJ1Bq0UmhGuF/t7K6ZOMaerjICCpruoRfFCgKiHQ7ZwEmgquliI+tlq5cpWIYIylIrrZ2ExHxlEpRxEeFg2ol2UlOkihfWmmxnuRIWF4vo0sR9HX4pFwtUJGUYllIgZW7Uci4+EWVsNIKH+M1dhP8MxX9AO0zrXbVYsDRqGLCtRIhPMS73RvoPNy9Hfo5urmonilPGBZ2FQNJTQhZlFFKS4kEYZu9nPQiginKaMBzvvVEoEm2lBaEpys6rgWMYx1XPzKx0cpH5Sq28MESwtApU1YJY0JgLJWIqWQGyybPAVNzoKmWPJV1cPhddnFivlgWAra6GtQSmQw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FjZbG9S7BmVTwt3lnX96fnP11v/L6fVPF+hQpIT7eUI0pPK4zIz2rUxJi9FR0Yd9SyepdfjsIb+V8g+rLEjrACwAAA==' | base64 --decode | tee /etc/nginx/nginxconfig.io-xgqfrms.xyz.tar.gz > /dev/null

# Navigate to your NGINX configuration directory on your server:
$ cd /etc/nginx

# Create a backup of your current NGINX configuration:
$ tar -czvf nginx_$(date +'%F_%H-%M-%S').tar.gz nginx.conf sites-available/ sites-enabled/ nginxconfig.io/

# Extract the new compressed configuration archive using tar:
$ tar -xzvf nginxconfig.io-xgqfrms.xyz.tar.gz

Let's Encrypt

# Generate Diffie-Hellman keys by running this command on your server:
$ openssl dhparam -out /etc/nginx/dhparam.pem 2048

# Create a common ACME-challenge directory (for Let's Encrypt):
$ mkdir -p /var/www/_letsencrypt
$ chown www-data /var/www/_letsencrypt

# Comment out SSL related directives in the configuration:
$ sed -i -r 's/(listen .*443)/\1;#/g; s/(ssl_(certificate|certificate_key|trusted_certificate) )/#;#\1/g' /etc/nginx/sites-available/xgqfrms.xyz.conf

# Reload your NGINX server:
$ sudo nginx -t && sudo systemctl reload nginx

# Obtain SSL certificates from Let's Encrypt using Certbot:
$ certbot certonly --webroot -d xgqfrms.xyz --email info@xgqfrms.xyz -w /var/www/_letsencrypt -n --agree-tos --force-renewal

# Uncomment SSL related directives in the configuration:
$ sed -i -r 's/#?;#//g' /etc/nginx/sites-available/xgqfrms.xyz.conf

# Reload your NGINX server:
$ sudo nginx -t && sudo systemctl reload nginx

# Configure Certbot to reload NGINX when it successfully renews certificates:
$ echo -e '#!/bin/bash\nnginx -t && systemctl reload nginx' | sudo tee /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
$ sudo chmod a+x /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh


Let's go live! 🎉

# Reload NGINX to load in your new configuration:
$ sudo nginx -t && sudo systemctl reload nginx

configs

/etc/nginx/nginx.conf

# Generated by nginxconfig.io
# https://www.digitalocean.com/community/tools/nginx?domains.0.server.domain=xgqfrms.xyz&global.https.ocspCloudflareType=both&global.https.ocspGoogleType=both&global.https.ocspOpenDnsType=both

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_ciphers          ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-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 [2606:4700:4700::1111] [2606:4700:4700::1001] 8.8.8.8 8.8.4.4 [2001:4860:4860::8888] [2001:4860:4860::8844] 208.67.222.222 208.67.220.220 [2620:119:35::35] [2620:119:53::53] valid=60s;
    resolver_timeout     2s;

    # Load configs
    include              /etc/nginx/conf.d/*.conf;
    include              /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/xgqfrms.xyz.conf

server {
    listen                  443 ssl http2;
    listen                  [::]:443 ssl http2;
    server_name             xgqfrms.xyz;
    set                     $base /var/www/xgqfrms.xyz;
    root                    $base/public;

    # SSL
    ssl_certificate         /etc/letsencrypt/live/xgqfrms.xyz/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/xgqfrms.xyz/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/xgqfrms.xyz/chain.pem;

    # security
    include                 nginxconfig.io/security.conf;

    # index.php
    index                   index.php;

    # index.php fallback
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    # handle .php
    location ~ \.php$ {
        include nginxconfig.io/php_fastcgi.conf;
    }
}

# subdomains redirect
server {
    listen                  443 ssl http2;
    listen                  [::]:443 ssl http2;
    server_name             *.xgqfrms.xyz;

    # SSL
    ssl_certificate         /etc/letsencrypt/live/xgqfrms.xyz/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/xgqfrms.xyz/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/xgqfrms.xyz/chain.pem;
    return                  301 https://xgqfrms.xyz$request_uri;
}

# HTTP redirect
server {
    listen      80;
    listen      [::]:80;
    server_name .xgqfrms.xyz;
    include     nginxconfig.io/letsencrypt.conf;

    location / {
        return 301 https://xgqfrms.xyz$request_uri;
    }
}

/etc/nginx/nginxconfig.io/letsencrypt.conf

# ACME-challenge
location ^~ /.well-known/acme-challenge/ {
    root /var/www/_letsencrypt;
}

/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" always;

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

/etc/nginx/nginxconfig.io/general.conf

# favicon.ico
location = /favicon.ico {
    log_not_found off;
    access_log    off;
}

# robots.txt
location = /robots.txt {
    log_not_found off;
    access_log    off;
}

# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
    expires    7d;
    access_log off;
}

# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
    add_header Access-Control-Allow-Origin "*";
    expires    7d;
    access_log off;
}

# gzip
gzip            on;
gzip_vary       on;
gzip_proxied    any;
gzip_comp_level 6;
gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

/etc/nginx/nginxconfig.io/php_fastcgi.conf

# 404
try_files                     $fastcgi_script_name =404;

# default fastcgi_params
include                       fastcgi_params;

# fastcgi settings
fastcgi_pass                  unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index                 index.php;
fastcgi_buffers               8 16k;
fastcgi_buffer_size           32k;

# fastcgi params
fastcgi_param DOCUMENT_ROOT   $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/";



refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-09-03 11:06  xgqfrms  阅读(738)  评论(1编辑  收藏  举报