简体   繁体   中英

nginx - two domains through nginx reverse proxy

I'm currently working on creating a dockerized server with two sites. I want them both to run over port 443. So far, I've managed to get one of them running on their own using the nginx reverse proxy, but when I try to do both, it seems to be totally ignoring my server.

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;
}
}

When running just one server, this config gile was just one of the larger server sections, which worked perfectly. But when trying to create the set up I'm trying to create (diagram below), it instantly redirects to the API on my accept environment. My guess as to why this specific api is because it's the next available line with the same domain in my window's hosts file, so the browser gets told to go there(?). 在此处输入图像描述

For any further information that I forgot to give, please ask.

I have similar function but I do have different "servers" listening to different server_name configuration

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;
    }

}

You could encapsulate the server name inside the desired block and then set the correct proxy_pass to backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM