NGINX 本地开发指南

在 localhost 上运行 NGINX,默认端口 80/8080,作为反向代理或静态文件服务器访问本地项目。


NGINX 是高性能 Web 服务器与反向代理,常用于生产环境。本地开发中可用于托管静态站点、反向代理 Node.js / Python 后端,或替代 Apache 运行 PHP(需 PHP-FPM)。

默认 localhost 访问

用途地址
默认站点http://localhost
自定义端口http://localhost:8080

默认监听 80 端口,可在 nginx.conf 中修改 listen

安装

  • macOSbrew install nginx
  • Linuxsudo apt install nginxsudo yum install nginx
  • Windows:从 nginx.org 下载解压使用

网站根目录

默认静态文件目录(因系统而异):

  • Linux:/usr/share/nginx/html//var/www/html/
  • macOS (Homebrew):/usr/local/var/www/

server 块中配置 root 指向项目目录:

server {
    listen 80;
    server_name localhost;
    root /path/to/your/project;
    index index.html index.htm;
}

反向代理示例(Node.js)

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
}

应用跑在 3000 端口时,通过 http://localhost 统一入口访问。

常用命令

nginx -t          # 测试配置
nginx -s reload     # 重载配置
sudo nginx          # 启动
sudo nginx -s stop  # 停止

常见问题

502 Bad Gateway
后端服务(如 Node、PHP-FPM)未启动或 proxy_pass 地址错误。

403 Forbidden
检查 root 路径权限与 index 文件是否存在。

端口冲突
改用 listen 8080,访问 **http://localhost:8080**。

小结

NGINX 适合本地静态站点、API 反向代理与多项目虚拟主机。配置完成后通过 http://localhost 访问本地服务。

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run