Swift Perfect服务搭配Nginx反向代理配置问题求助
Hey there! Let’s figure out why your Nginx reverse proxy isn’t working as expected for your Perfect Framework app. Since direct access to 127.0.0.1:8080 works perfectly, the issue is almost certainly in your Nginx configuration details (not a fundamental problem with Perfect itself)—even though you’ve set up similar proxies for Vue/Node before, Perfect has a few specific requirements we might have missed.
Here are the most likely gaps in your setup, along with fixes:
1. Missing Critical Proxy Headers
Perfect relies on certain HTTP headers to correctly process incoming requests, especially when behind a reverse proxy. Unlike some Node/Vue apps that might ignore these, Perfect needs them to handle things like request routing and client IP detection. Add these lines inside your location / block:
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;
2. Incorrect proxy_pass Format
A common mistake is forgetting to include the http:// prefix in your proxy_pass directive. Perfect runs an HTTP server, so Nginx needs to explicitly use the HTTP protocol to forward requests. Make sure your line looks like this:
proxy_pass http://127.0.0.1:8080;
Not just proxy_pass 127.0.0.1:8080;—the missing http:// will break the proxy entirely.
3. HTTP Version Mismatch
By default, Nginx uses HTTP/1.0 for proxy requests, but Perfect is optimized for HTTP/1.1. Adding these lines to your location block ensures compatibility and prevents connection drops:
proxy_http_version 1.1; proxy_set_header Connection ""; # Disables outdated keep-alive headers that cause conflicts
4. Forgetting to Validate or Reload Nginx Config
Even if you fix the config, Nginx won’t pick up changes until you reload it. First, test for syntax errors to avoid breaking your server:
sudo nginx -t
If you see "test is successful", reload the service to apply changes:
sudo systemctl reload nginx
5. Quick Permission/Firewall Check
While less likely (since direct access works), double-check that Nginx has permission to connect to 127.0.0.1:8080. On Ubuntu, ensure no firewall rules block loopback traffic, and that the user running Nginx (usually www-data) can reach the port.
Example Working Nginx Config
Putting it all together, here’s a complete server block you can adapt to your setup:
server { listen 80; server_name your-domain.com localhost; # Replace with your actual domain or localhost location / { proxy_pass http://127.0.0.1:8080; 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; proxy_http_version 1.1; proxy_set_header Connection ""; } # Optional: Let Nginx handle static files directly for better performance # location /public/ { # root /path/to/your/perfect/app; # expires 30d; # } }
How to Debug Further
If you’re still stuck:
- Check Nginx’s error log for specific clues:
tail -f /var/log/nginx/error.log - Review your Perfect app’s logs to confirm if it’s receiving requests from Nginx at all
- Test with
curl -I http://localhostto inspect the headers Nginx is returning
9 times out of 10, the fix is adding those missing proxy headers or correcting the proxy_pass format. Perfect itself is working fine since direct access works—we just need to make sure Nginx is forwarding requests correctly.
内容的提问来源于stack exchange,提问作者Burf2000

