<strong>
<strong>
前因
关于Nginx部署、配置的文章网上已经发布过很多,包括我自己也私藏了不少还发布过两篇:
- 后端必备 Nginx 配置
- 前端必备 Nginx 配置
整理出来为的就是需要的时候,复制、粘贴就能使用。
然而千奇百怪的实际开发中,你肯定需要增删Nginx配置。你就得上网搜一下,复制粘贴出bug了又得调一下...
搞定还得保存下来以备后患。多了不好找还得整理...就搞得很麻烦
后果
今天我给大家推荐一款"Nginx配置利器",配配变量就能一键生成常用配置。和繁琐低效配置说再见
- 网站链接:nginxconfig 在线配置网站
- nginxconfig github项目
nginxconfig 目前支持:
- Angular、React、Vue、Node.js
- PHP、Python
- wordpress、Magento、Drupal
- 缓存、Https、日志等各种配置...
使用
实现用户访问*.myweb.com域名自动跳转到myweb.com配置,并且开启http强制跳转到https的配置。
配置完之后,下方还有安装步骤指导你配置生效。交互体验相当好
生成配置 /etc/nginx/sites-available/myweb.com.conf 如下:
server {
\tlisten 443 ssl http2;
\tlisten [::]:443 ssl http2;
\tserver_name myweb.com;
\troot /var/www/myweb.com/public;
\t# SSL
\tssl_certificate /etc/letsencrypt/live/myweb.com/fullchain.pem;
\tssl_certificate_key /etc/letsencrypt/live/myweb.com/privkey.pem;
\tssl_trusted_certificate /etc/letsencrypt/live/myweb.com/chain.pem;
\t# security
\tinclude nginxconfig.io/security.conf;
\t# index.html fallback
\tlocation / {
\t\ttry_files $uri $uri/ /index.html;
\t}
\t# additional config
\tinclude nginxconfig.io/general.conf;
}
# subdomains redirect
server {
\tlisten 443 ssl http2;
\tlisten [::]:443 ssl http2;
\tserver_name *.myweb.com;
\t# SSL
\tssl_certificate /etc/letsencrypt/live/myweb.com/fullchain.pem;
\tssl_certificate_key /etc/letsencrypt/live/myweb.com/privkey.pem;
\tssl_trusted_certificate /etc/letsencrypt/live/myweb.com/chain.pem;
\treturn 301 https://myweb.com$request_uri;
}
# HTTP redirect
server {
\tlisten 80;
\tlisten [::]:80;
\tserver_name .myweb.com;
\tinclude nginxconfig.io/letsencrypt.conf;
\tlocation / {
\t\treturn 301 https://myweb.com$request_uri;
\t}
}
网站下方还罗列了推荐的nginx配置、安全配置...以作参考
/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 {
\tmulti_accept on;
\tworker_connections 65535;
}
http {
\tcharset utf-8;
\tsendfile on;
\ttcp_nopush on;
\ttcp_nodelay on;
\tserver_tokens off;
\tlog_not_found off;
\ttypes_hash_max_size 2048;
\tclient_max_body_size 16M;
\t# MIME
\tinclude mime.types;
\tdefault_type application/octet-stream;
\t# logging
\taccess_log /var/log/nginx/access.log;
\terror_log /var/log/nginx/error.log warn;
\t# SSL
\tssl_session_timeout 1d;
\tssl_session_cache shared:SSL:10m;
\tssl_session_tickets off;
\t# Diffie-Hellman parameter for DHE ciphersuites
\tssl_dhparam /etc/nginx/dhparam.pem;
\t# Mozilla Intermediate configuration
\tssl_protocols TLSv1.2 TLSv1.3;
\tssl_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;
\t# OCSP Stapling
\tssl_stapling on;
\tssl_stapling_verify on;
\tresolver 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;
\tresolver_timeout 2s;
\t# load configs
\tinclude /etc/nginx/conf.d/*.conf;
\tinclude /etc/nginx/sites-enabled/*;
}
/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) {
\tdeny all;
}
拓展
以上就满足日常开发需求啦。如果你压抑不住,想要展示你的高端操作。
你可以加入到项目本身开发中;nginxconfig项目本身是MIT开源协议,你也可以在此基础上迭代出自己的版本
原文链接:https://juejin.im/post/5dbb88e56fb9a0208055c5fa
閱讀更多 追逐仰望星空 的文章