dns - nginx 管理本地服务器依赖于传入域名

标签 dns nginx

我有几个应用程序在不同的端口本地运行,我如何配置 NGINX 服务器以将请求从端口 80 转发到我的应用程序取决于收入域名。例如,端口 8181 上名为“app1”的 2 个本地应用程序,如果请求来自 http://app1.com - nginx 转发到 http://localhost:8181

我看过 nginx 文档,如果有人这样做,我会询问你的例子。 谢谢

最佳答案

假设您要创建一个 reverse proxy ,我的方法是先在名为/etc/nginx/reverse-proxy.conf的新文件中配置以下反向代理设置:

# Serve / from local http server.
# Just add the following to individual vhost configs:
# proxy_pass http://localhost:3001/;

proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;

然后,对于我正在配置的每个反向代理,我在 /etc/nginx/sites-enabled 中添加一个适当命名的配置文件,内容如下:

server {
    server_name app1.com;
    server_name www.app1.com;
    location / {
        include /etc/nginx/reverse-proxy.conf;
        proxy_pass http://localhost:8181/;
    }
}

您可以根据需要创建任意数量的 server block ,并将它们指向不同的本地(甚至远程)应用程序服务器。您还可以添加 location block 以静态地在同一域内或从不同的本地应用程序服务器提供不同的 URL。

(您也可以将所有配置滚动到 /etc/nginx/nginx.conf,但我发现将配置分成多个文件更容易。)

关于dns - nginx 管理本地服务器依赖于传入域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15546792/

相关文章:

django - 无法通过域 URL 访问 Nginx 站点

Kubernetes - 如何在集群级别设置 DNS 策略

c# - 要属性(property),还是不要属性(property)?

azure - VIP 交换和持久性 - VIP 何时被回收?

nginx - 使用 Nginx (ngx_http_ssl_module) 编译时,: Openssl, LibreSSL 或 BoringSSL 哪个更好?

nginx - 在 Centos 6.8 上升级 nginx

ssl - webroot 不会检测 Let's Encrypt 证书创建的根路径

python - 使用 Python 和 Twisted 的 DNS 服务器 - 异步操作的问题

php - Amazon Linux 上的多个 PHP-FPM 版本加载相同的配置

python - 使用 django、virtualenv、gunicorn 和 nginx 设置服务器以托管多个域