编写nginx https访问配置文件
nginx配置文件路径
/etc/nginx/conf.d
进入配置文件路径
cd /etc/nginx/conf.d
编辑配置文件
vim example.conf
nginx 配置域名证书,https访问。 参考模板:
# 阻止IP直接访问 server { listen 80 default_server; listen 443 ssl default_server; server_name _; # SSL证书 ssl_certificate /etc/nginx/ssl/test.pem; ssl_certificate_key /etc/nginx/ssl/test.key; # SSL安全配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305; ssl_prefer_server_ciphers off; # 安全头 add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; # 记录IP直接访问 access_log /var/log/nginx/ip_direct_access.log; return 444; } # HTTP重定向到HTTPS server { listen 80; server_name test.cn www.test.cn; # 安全头 add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; return 301 https://$server_name$request_uri; } # HTTPS服务器 - VanBlog 主站 server { listen 443 ssl http2; server_name test.cn test.cn; # SSL证书配置 ssl_certificate /etc/nginx/ssl/test.pem; ssl_certificate_key /etc/nginx/ssl/test.key; # SSL安全配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305; ssl_prefer_server_ciphers off; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # 安全头 add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header Referrer-Policy "strict-origin-when-cross-origin"; # VanBlog 主站 location / { proxy_pass http://172.25.0.3:80; # 代理头设置 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; # 超时设置 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; # 缓冲区优化 proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; } # 静态资源缓存优化 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { proxy_pass http://172.25.0.3:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 缓存设置 expires 1y; add_header Cache-Control "public, immutable"; add_header Vary "Accept-Encoding"; } # 禁止访问敏感文件 location ~ /\. { deny all; access_log off; log_not_found off; } # 健康检查端点 location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } }
本文作者:小白
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!