我来帮您优化nginx配置,实现通过 https://jinhuitech.top 访问您的官网。配置将包括HTTPS设置、域名绑定和安全优化。
由于文件系统限制,我直接为您提供优化配置的部署步骤:

🚀 部署步骤
1. 创建网站目录和备份当前配置
# 创建网站目录
sudo mkdir -p /etc/www/website
# 备份当前nginx配置
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
2. 创建SSL证书目录
# 创建SSL证书目录
sudo mkdir -p /etc/nginx/ssl
3. 获取SSL证书(推荐使用Let's Encrypt)
# 安装certbot
sudo yum install -y certbot python3-certbot-nginx
# 获取SSL证书
sudo certbot --nginx -d jinhuitech.top -d www.jinhuitech.top
# 或者手动获取证书
sudo certbot certonly --nginx -d jinhuitech.top -d www.jinhuitech.top
4. 更新nginx配置
将当前的nginx.conf替换为优化配置:
# 创建新的nginx配置
sudo tee /etc/nginx/nginx.conf > /dev/null << 'EOF'
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
client_max_body_size 100M;
# Gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# HTTP重定向到HTTPS
server {
listen 80;
listen [::]:80;
server_name jinhuitech.top www.jinhuitech.top;
return 301 https://$server_name$request_uri;
}
# HTTPS服务器
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name jinhuitech.top www.jinhuitech.top;
root /etc/www/website;
index index.html index.htm;
# SSL配置 - 使用Let's Encrypt证书
ssl_certificate /etc/letsencrypt/live/jinhuitech.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jinhuitech.top/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# 安全头
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|txt)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
location / {
try_files $uri$uri/ /index.html;
}
# 隐藏nginx版本
server_tokens off;
# 错误页面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
}
EOF
5. 部署网站文件
# 将您的网站文件上传到 /etc/www/website/ 目录
# 确保有index.html文件
sudo chown -R nginx:nginx /etc/www/website
sudo chmod -R 755 /etc/www/website
6. 测试和启动nginx
# 测试nginx配置
sudo nginx -t
# 重启nginx
sudo systemctl restart nginx
# 检查状态
sudo systemctl status nginx
7. 配置防火墙
# 开放HTTP和HTTPS端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# 或者手动开放端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
8. 域名解析配置
确保您的域名DNS解析指向服务器IP:
-
A记录:
jinhuitech.top→47.238.98.71 -
CNAME记录:
www.jinhuitech.top→jinhuitech.top
🔧 SSL证书自动续期
# 设置Let's Encrypt自动续期
sudo crontab -e
# 添加以下行(每天检查证书并自动续期)
0 12 * * * /usr/bin/certbot renew --quiet
📋 验证部署
部署完成后,验证配置:
# 检查nginx配置
sudo nginx -t
# 检查SSL证书
openssl s_client -connect jinhuitech.top:443 -servername jinhuitech.top
# 测试HTTP重定向
curl -I http://jinhuitech.top
# 测试HTTPS访问
curl -I https://jinhuitech.top
🚨 重要提醒
-
SSL证书路径:配置中使用了Let's Encrypt证书路径,如果使用其他证书,请修改路径
-
域名解析:确保域名已正确解析到您的服务器IP
-
网站文件:将您的静态网站文件放置在
/etc/www/website/目录 -
阿里云安全组:确保开放了80和443端口
配置完成后,您就可以通过 https://jinhuitech.top 访问您的官网了!
IT技术交流群:

1596

被折叠的 条评论
为什么被折叠?



