大网数据广告活动
服务器运维问答

Nginx: use uwsgi_pass with a proxy_pass fallback?

· 52CE 我爱测

Is it possible to configure Nginx to proxy to a uwsgi server ( uwsgi_pass ), falling back to a standard http proxy ( proxy_pass ) if the uwsgi server is not available? My development environment runs as a standard HTTP server on …

问题原文

原帖链接 · ibrewster · CC BY-SA 3.0

Is it possible to configure Nginx to proxy to a uwsgi server (uwsgi_pass), falling back to a standard http proxy (proxy_pass) if the uwsgi server is not available?

My development environment runs as a standard HTTP server on a given port, but in production it will be running under a uWSGI socket. I would like to be able to develop my app, then (still on my development machine) launch the the app under a uwsgi server for testing without having to switch my nginx config as well each time. If nginx could be configured to try with the uwsgi_pass, but if it got an error (because the uwsgi server is not running) fall back to the proxy_pass, that would be ideal.

I could also see this being handy in a production environment: normally, just proxy to the local uwsgi socket, but if for some reason that's not working (for some arbitrary definition of not working), proxy to some secondary http server somewhere else. However, for me for now it's just to make development more convenient.

已采纳解答

原帖链接 · Drifter104 · CC BY-SA 3.0

Assuming you're going to get a 502 timeout error from the uWSGI backend the following should do what you are looking for.

uwsgi_intercept_errors on;
error_page 502 = @fallback;

location / {
    uwsgi_pass uwsgi://192.168.0.1:9000;
    <rest of your config>
    }

location @fallback {
    proxy_pass http://192.168.0.2;
    <rest of your config>
    }

Obviously substitute in your own values but these are the important parts. If you find you get a different http response you can edit as needed.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。