nginx-通过nginx反向代理的两个域

时间:2019-10-01 08:55:19

标签: nginx reverse-proxy

我目前正在创建具有两个站点的dockerized服务器。我希望它们都在端口443上运行。到目前为止,我已经设法使用nginx反向代理使它们中的一个独立运行,但是当我尝试同时执行这两个操作时,似乎完全忽略了我的服务器。 / p>

stream {
upstream shop_local_xposi_com {
    server 127.0.0.1:9000;
}

upstream sockets_local_xposi_com {
    server 127.0.0.1:9001;
}

map $ssl_preread_server_name $upstream {
    shop.local.xposi.com shop_local_website_com;
    socket.local.xposi.com sockets_local_website_com;
}

# SHOP webserver
server {
    # SSL
    listen 127.0.0.1:9000 ssl;
    ssl_certificate /etc/nginx/certs/website.com.crt;
    ssl_certificate_key /etc/nginx/certs/website.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    ssl_prefer_server_ciphers on;

    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

# SOCKET webserver
server {
    # SSL
    listen 127.0.0.1:9001 ssl;
    ssl_certificate /etc/nginx/certs/website.com.crt;
    ssl_certificate_key /etc/nginx/certs/website.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    ssl_prefer_server_ciphers on;

    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass socket:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

server {
    listen 443;
    ssl_preread on;
    proxy_pass $upstream;
}
}

仅运行一台服务器时,此配置gile只是较大的服务器部分之一,效果很好。但是,当尝试创建我要创建的设置(如下图)时,它会立即重定向到我的接受环境上的API。我对为什么使用此特定api的猜测是,因为它是我的窗口的hosts文件中具有相同域的下一个可用行,所以告诉浏览器去那里(?)。enter image description here

有关我忘记提供的其他信息,请询问。

1 个答案:

答案 0 :(得分:0)

我具有相似的功能,但确实有不同的“服务器”在监听不同的server_name配置

server {
    listen 80 ; (or listen 443 ;)

    server_name     shop-local.website.com ;
    location / {
        ... some code
        proxy_pass http://shoplocalwebsiteIP:port;
    }

}

server {
    listen 80 ; (or listen 443 ;)

    server_name     socket-local.website.com ;
    location / {
        ... some code
        proxy_pass http://socketlocalwebsiteIP:port;
    }

}

您可以将服务器名称封装在所需的块中,然后将正确的proxy_pass设置为后端。