NGINX Local Development Guide

Run NGINX on localhost, default ports 80/8080, to access local projects as a reverse proxy or static file server.


NGINX is a high-performance web server and reverse proxy commonly used in production environments. In local development, it can be used to host static sites, reverse proxy Node.js/Python backends, or replace Apache for running PHP (requires PHP-FPM).

Default localhost Access

PurposeAddress
Default Sitehttp://localhost
Custom Porthttp://localhost:8080

By default, it listens on port 80, which can be modified in nginx.conf by changing the listen directive.

Installation

  • macOS: brew install nginx
  • Linux: sudo apt install nginx or sudo yum install nginx
  • Windows: Download and extract from nginx.org

Website Root Directory

Default static file directories (varies by system):

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

Configure the root directive in the server block to point to your project directory:

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

Reverse Proxy Example (Node.js)

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

When the application runs on port 3000, access it through the unified entry point at http://localhost.

Common Commands

nginx -t          # Test configuration
nginx -s reload    # Reload configuration
sudo nginx         # Start
sudo nginx -s stop # Stop

Frequently Asked Questions

502 Bad Gateway
The backend service (e.g., Node, PHP-FPM) is not running or the proxy_pass address is incorrect.

403 Forbidden
Check the permissions of the root path and whether the index file exists.

Port Conflict
Change to listen 8080 and access http://localhost:8080.

Summary

NGINX is suitable for local static sites, API reverse proxies, and multi-project virtual hosting. Once configured, access your local services through http://localhost.

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