Nginx向应用服务器发送请求的方式有哪些?求含proxy_pass等的完整列表
Great question! Nginx has a solid set of built-in ways to route traffic to your application servers, tailored to different protocols your app might speak. Let’s break down all the main supported methods clearly:
HTTP/HTTPS Proxying (
proxy_pass)
This is the most widely used method for forwarding requests to HTTP-based apps (like Node.js services, Python Flask/Django apps running an HTTP server, Java Spring Boot apps, etc.). You define a location block and useproxy_passto point to your app's HTTP endpoint—this can be a local port, remote IP, or domain. Example config:location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }FastCGI Pass (
fastcgi_pass)
Built for apps that use the FastCGI protocol, most commonly PHP applications (WordPress, Laravel, etc.). You’ll pair this withfastcgi_paramto pass critical environment variables to services like PHP-FPM. Example:location ~ \.php$ { fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Or tcp://localhost:9000 for TCP connections fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }uWSGI Pass (
uwsgi_pass)
Designed specifically for Python apps using the uWSGI protocol (a common setup for Django/Flask deployed with uWSGI servers). Nginx can communicate with uWSGI via TCP or Unix sockets. Example:location / { uwsgi_pass unix:/path/to/your/app.sock; # Or tcp://localhost:8000 include uwsgi_params; }SCGI Pass (
scgi_pass)
A lighter alternative to FastCGI, using the SCGI protocol. It’s used for legacy or lightweight apps (some Perl/Python services) that implement SCGI. Example:location /scgi-endpoint { scgi_pass localhost:9000; include scgi_params; }gRPC Proxying (
grpc_pass)
For apps built with gRPC (a high-performance RPC framework), Nginx supports forwarding gRPC requests over HTTP/2. You’ll need to enable HTTP/2 in your server block first. Example:server { listen 443 ssl http2; ssl_certificate /path/to/ssl/cert.pem; ssl_certificate_key /path/to/ssl/key.pem; location /mygrpc.Service/ { grpc_pass grpc://localhost:50051; } }WebSocket Proxying
While there’s no dedicated "websocket_pass" directive, Nginx can proxy WebSocket connections by upgrading the HTTP connection. Useproxy_passalongside specific headers to handle the handshake:location /ws { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }Memcached Proxying (
memcached_pass)
If your app uses Memcached for caching, Nginx can directly forward requests to the Memcached server to serve cached content without hitting your app server. You can add a fallback to your app if the cache misses:location /cached-content { set $memcached_key $uri; memcached_pass localhost:11211; error_page 404 = @fallback_to_app; } location @fallback_to_app { proxy_pass http://localhost:3000; }
These are all the primary built-in methods Nginx provides to connect to application servers or backend services. The right choice depends entirely on the protocol your application is configured to use.
内容的提问来源于stack exchange,提问作者Pablo Tato Ramos

