怎么设置Ubuntu24 纯 Nginx 完整的 server 站点配置?
美国、香港服务器
怎么设置Ubuntu24 纯 Nginx 完整的 server 站点配置?
10-15 来源:
1. 创建网站根目录
sudo mkdir -p /var/www/yourdomain
sudo chown -R www-data:www-data /var/www/yourdomain
sudo chmod -R 755 /var/www/yourdomain
# 放一个测试首页
echo "Hello Site" | sudo tee /var/www/yourdomain/index.html
2. 准备 SSL 证书(推荐 Let’s Encrypt,certbot)
sudo apt update && sudo apt install certbot python3-certbot-nginx -y
## 1、新建站点配置文件
sudo nano /etc/nginx/sites-available/yourdomain
粘贴下面完整配置,**把 `yourdomain.com` 替换成你自己域名**
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
# HTTP全部301重定向HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
# SSL证书(certbot自动生成,不要手动改路径)
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# SSL安全参数
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
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;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# 网站根目录
root /var/www/yourdomain;
index index.html index.htm index.php;
# 日志(使用前面全局定义的cf_log格式,查看真实访客IP)
access_log /var/log/nginx/yourdomain-access.log cf_log;
error_log /var/log/nginx/yourdomain-error.log warn;
# 基础头部安全
add_header X-Frame-Options SAMEORIGIN;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options nosniff;
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2|woff|ttf)$ {
expires 7d;
add_header Cache-Control "public, no-transform";
}
# 禁止访问隐藏文件 .开头
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# PHP支持(如果是静态网站,可直接删除这段location)
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.html;
}
}
## 2、启用站点,禁用默认站点
# 创建软链接启用站点
sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled/
# 删除默认站点(可选,防止冲突)
sudo rm /etc/nginx/sites-enabled/default
## 3、申请 HTTPS 证书(certbot)
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
注意:CF 后台 SSL 模式设置为 `Full (strict)`,源站必须有合法证书,不能自签名。
## 4、校验配置并重载 Nginx
sudo nginx -t
sudo systemctl reload nginx
# 配套全局配置回顾(/etc/nginx/nginx.conf 的 http 块)
这部分是全局,**只需要配置一次**,负责 CF-Connecting-IP
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 引入Cloudflare真实IP配置
include /etc/nginx/cf-realip.conf;
# 自定义日志格式
log_format cf_log "$remote_addr [$time_local] "$request" $status "$http_referer" "$http_user_agent" CF_RAW:$http_cf_connecting_ip";
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/sites-enabled/*;
}
```
# 【必加】default_server 拦截直接访问源 IP
新建 `/etc/nginx/sites-available/block-direct-ip`
```
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
return 444;
}
启用:
sudo ln -s /etc/nginx/sites-available/block-direct-ip /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
作用:用户直接用服务器公网 IP 访问网站,直接断开连接返回 444,保护源站。
# 验证命令
# 实时看访问日志,确认$remote_addr是访客真实IP
sudo tail -f /var/log/nginx/yourdomain-access.log
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快