当前位置:   article > 正文

Nginx 的相关配置_nginx permissions-policy

nginx permissions-policy

使用tar解压新的压缩配置

tar -xzvf nginxconfig.io-example.com.tar.gz | xargs chmod 0644
  • 在您的服务器上运行此命令生成Diffie-Hellman keys:

     
    openssl dhparam -out /etc/nginx/dhparam.pem 2048

创建一个通用的ACME-challenge目录(用于 Let's Encrypt):

mkdir -p /var/www/_letsencrypt
chown www-data /var/www/_letsencrypt
  • 注释掉配置中的SSL相关指令:

    sed -i -r 's/(listen .*443)/\1; #/g; s/(ssl_(certificate|certificate_key|trusted_certificate) )/#;#\1/g; s/(server \{)/\1\n    ssl off;/g' /etc/nginx/sites-available/example.com.conf
  • 重新加载你的NGINX服务器:

    sudo nginx -t && sudo systemctl reload nginx
  • 使用Certbot从 Let's Encrypt 获得SSL证书:

    certbot certonly --webroot -d example.com --email info@example.com -w /var/www/_letsencrypt -n --agree-tos --force-renewal
  • 在配置中取消注释SSL相关指令:

    sed -i -r -z 's/#?; ?#//g; s/(server \{)\n    ssl off;/\1/g' /etc/nginx/sites-available/example.com.conf
  • 重新加载你的NGINX服务器:

    sudo nginx -t && sudo systemctl reload nginx

配置Certbot,当NGINX成功更新证书时重新加载:

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

重新加载NGINX以载入新的配置:

sudo nginx -t && sudo systemctl reload nginx

配置文件

/etc/nginx/nginx.conf

  1. # Generated by nginxconfig.io
  2. # https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
  3. user www-data;
  4. pid /run/nginx.pid;
  5. worker_processes auto;
  6. worker_rlimit_nofile 65535;
  7. # Load modules
  8. include /etc/nginx/modules-enabled/*.conf;
  9. events {
  10. multi_accept on;
  11. worker_connections 65535;
  12. }
  13. http {
  14. charset utf-8;
  15. sendfile on;
  16. tcp_nopush on;
  17. tcp_nodelay on;
  18. server_tokens off;
  19. log_not_found off;
  20. types_hash_max_size 2048;
  21. types_hash_bucket_size 64;
  22. client_max_body_size 16M;
  23. # MIME
  24. include mime.types;
  25. default_type application/octet-stream;
  26. # Logging
  27. access_log /var/log/nginx/access.log;
  28. error_log /var/log/nginx/error.log warn;
  29. # SSL
  30. ssl_session_timeout 1d;
  31. ssl_session_cache shared:SSL:10m;
  32. ssl_session_tickets off;
  33. # Diffie-Hellman parameter for DHE ciphersuites
  34. ssl_dhparam /etc/nginx/dhparam.pem;
  35. # Mozilla Intermediate configuration
  36. ssl_protocols TLSv1.2 TLSv1.3;
  37. 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;
  38. # OCSP Stapling
  39. ssl_stapling on;
  40. ssl_stapling_verify on;
  41. 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;
  42. resolver_timeout 2s;
  43. # Load configs
  44. include /etc/nginx/conf.d/*.conf;
  45. include /etc/nginx/sites-enabled/*;
  46. }

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

  1. server {
  2. listen 443 ssl http2;
  3. listen [::]:443 ssl http2;
  4. server_name example.com;
  5. set $base /var/www/example.com;
  6. root $base/public;
  7. # SSL
  8. ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  9. ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  10. ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
  11. # security
  12. include nginxconfig.io/security.conf;
  13. # index.php
  14. index index.php;
  15. # index.php fallback
  16. location / {
  17. try_files $uri $uri/ /index.php?$query_string;
  18. }
  19. # additional config
  20. include nginxconfig.io/general.conf;
  21. # handle .php
  22. location ~ \.php$ {
  23. fastcgi_pass unix:/var/run/php/php-fpm.sock;
  24. include nginxconfig.io/php_fastcgi.conf;
  25. }
  26. }
  27. # subdomains redirect
  28. server {
  29. listen 443 ssl http2;
  30. listen [::]:443 ssl http2;
  31. server_name *.example.com;
  32. # SSL
  33. ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  34. ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  35. ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
  36. return 301 https://example.com$request_uri;
  37. }
  38. # HTTP redirect
  39. server {
  40. listen 80;
  41. listen [::]:80;
  42. server_name .example.com;
  43. include nginxconfig.io/letsencrypt.conf;
  44. location / {
  45. return 301 https://example.com$request_uri;
  46. }
  47. }

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

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

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

  1. # security headers
  2. add_header X-XSS-Protection "1; mode=block" always;
  3. add_header X-Content-Type-Options "nosniff" always;
  4. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  5. add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'; frame-ancestors 'self';" always;
  6. add_header Permissions-Policy "interest-cohort=()" always;
  7. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  8. # . files
  9. location ~ /\.(?!well-known) {
  10. deny all;
  11. }

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

  1. # favicon.ico
  2. location = /favicon.ico {
  3. log_not_found off;
  4. access_log off;
  5. }
  6. # robots.txt
  7. location = /robots.txt {
  8. log_not_found off;
  9. access_log off;
  10. }
  11. # assets, media
  12. 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)$ {
  13. expires 7d;
  14. access_log off;
  15. }
  16. # svg, fonts
  17. location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
  18. add_header Access-Control-Allow-Origin "*";
  19. expires 7d;
  20. access_log off;
  21. }
  22. # gzip
  23. gzip on;
  24. gzip_vary on;
  25. gzip_proxied any;
  26. gzip_comp_level 6;
  27. 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

  1. # 404
  2. try_files $fastcgi_script_name =404;
  3. # default fastcgi_params
  4. include fastcgi_params;
  5. # fastcgi settings
  6. fastcgi_index index.php;
  7. fastcgi_buffers 8 16k;
  8. fastcgi_buffer_size 32k;
  9. # fastcgi params
  10. fastcgi_param DOCUMENT_ROOT $realpath_root;
  11. fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
  12. fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/";
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/437139
推荐阅读
相关标签
  

闽ICP备14008679号