zh

在VPS上安装和配置Nginx:从入门到实用

Nginx是目前最流行的Web服务器之一,以高性能、低内存占用和出色的并发处理能力著称。无论是托管静态网站、WordPress,还是为Node.js或Python应用提供反向代理,Nginx都是首选方案。本指南介绍在VPS上完整配置Nginx的全部步骤。

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

安装Nginx

在Ubuntu/Debian系统上安装Nginx非常简单: apt update && apt install nginx -y 安装完成后,Nginx会自动启动。验证运行状态: systemctl status nginx 设置开机自启: systemctl enable nginx 在浏览器中访问您的服务器IP,若看到「Welcome to nginx!」页面,说明安装成功。 CentOS/AlmaLinux系统安装方式: dnf install nginx -y systemctl enable --now nginx firewall-cmd --permanent --add-service=http --add-service=https firewall-cmd --reload

配置虚拟主机(Server Block)

Nginx通过虚拟主机配置让一台服务器可以托管多个域名。为每个域名创建独立的配置文件: nano /etc/nginx/sites-available/example.com 填入以下配置模板(将example.com替换为您的域名): server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.html index.php; location / { try_files $uri $uri/ =404; } access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; } 创建网站根目录并启用配置: mkdir -p /var/www/example.com ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ nginx -t (测试配置语法) systemctl reload nginx

使用Let's Encrypt配置免费HTTPS证书

现代网站必须启用HTTPS。Certbot可自动为Nginx申请和续签Let's Encrypt免费证书。 安装Certbot: apt install certbot python3-certbot-nginx -y 为域名申请证书(需确保DNS已解析到服务器IP): certbot --nginx -d example.com -d www.example.com 按提示输入邮箱并同意服务条款,Certbot会自动修改Nginx配置启用HTTPS,并设置80端口跳转443。 证书有效期90天,Certbot会安装定时任务自动续签。验证自动续签: certbot renew --dry-run 续签成功后,您的网站就拥有了有效的HTTPS证书。

配置反向代理

Nginx常用于为后端应用(如Node.js、Python Flask、Django)提供反向代理。以下是代理本地3000端口应用的配置示例: server { listen 443 ssl; server_name app.example.com; ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem; location / { proxy_pass http://127.0.0.1:3000; 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; } } 代理配置中的请求头设置确保后端应用能正确获取客户端真实IP。AnubizHost离岸VPS提供充足的带宽资源,配合Nginx高效处理并发连接,是高流量应用的理想选择。

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online