三二互联专业提供速度最快最稳定的美国服务器、香港服务器。中美直连,亚洲优化![ 代理登陆 ] [ 付款方式 ] [ 找回密码 ][ 电子协议责任书 ]
硬件资源保障

采用高配品牌服务器

主流强悍CPU配置

确保服务高速稳定运行

中美直连线路

中美直连亚洲优化

采用中国CN2骨干网络

保证速度飞快稳定高效

怎么设置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速度最快

上一篇:如何在Ubuntu24.04上安装Nginx? 下一篇:如何在Nginx配置文件中使用CF‑Connecting‑IP?

美国GIA服务器三二互联版权所有 WWW.250.cc 2008-2015 All Rights Reserved
三二互联 - 专业的美国C3服务器、香港vps、抗DOOS流量清洗、云备份系统、网站加速系统、美国GIA服务器和香港云服务器产品提供商
三二互联24小时在线工单系统为您提供全面、专业、周到的技术支持与服务
咨询热线:400-679-9994(免长话费)