【问题标题】:Nginx Reverse proxy with no DNS for multiple websites多个网站没有 DNS 的 Nginx 反向代理
【发布时间】:2017-06-20 05:39:04
【问题描述】:

我在一个 ubuntu 16 服务器上有两个网站,我希望使用 nginx reverse proxygunicorn 让它们通过网络访问(Gunicorn 在内部为 127.0.0.1:8000127.0.0.1:8001 上的网站提供服务。

两个网站永远不会DNS指向我的服务器,并且两个必须在端口80下运行。所以问题是,如何为这些站点设置反向代理?我处于无法捕获主机名或其他端口以便用户进入特定站点的情况。

我的 first_website.conf:

upstream first_website {
    server unix:/var/www/first_website/first_website_env/run/gunicorn.sock 
    fail_timeout=0;
}

server {

    listen 80;

    # normally I would use different host name
    # to check, which site user wants to retrieve.
    server_name 123.12.34.789;

    client_max_body_size 4G;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:8001;
            break;
        }
    }
}

【问题讨论】:

  • 那么您打算如何区分服务器?你想让他们有不同的进入点吗?例如123.12.34.789/server1?
  • 是的 123.12.34.789/server1 将是一个不错的选择。但我不确定如何实现这一点,以及这是否是一个好习惯。

标签: nginx reverse-proxy gunicorn


【解决方案1】:

一个选项是将服务器放置在不同的 url 位置,例如:

upstream first_website {
    server unix:/var/www/first_website/first_website_env/run/gunicorn.sock 
    fail_timeout=0;
}

server {

    listen 80;

    server_name 123.12.34.789;

    client_max_body_size 4G;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location /server1/ {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:8000;
            break;
        }
    }

    location /server2/ {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:8001;
            break;
        }
    }
}

我相信这对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多