Two WordPress in single docker and access by reverse proxy Nginx

porpor asked:

Let’s say I have a droplet with Nginx and setup reverse proxy to specific docker container subfolders. below is my reverse proxy setup for nginx

Frontend Nginx

#For Domain 1
server {
    listen   80;
    server_name  domain1.com;

    location / {
        proxy_pass http://127.0.0.1:7000/domain1/;
        proxy_redirect     off;
        proxy_set_header   Host $host;
    }
}

#For Domain 2
server {
    listen   80;
    server_name  domain2.com;

    location / {
        proxy_pass http://127.0.0.1:7000/domain2/;
        proxy_redirect     off;
        proxy_set_header   Host $host;
    }
}

Backend Nginx

I setup single docker and add two wordpress websites in sub folders. Like below

/var/www /root directory
/var/www/domain1 //domain 1 website 
/var/www/domain2 //domain 2 website 

docker nginx conf.d file setup like below

server {
    listen 80;
    index index.php index.html;
    root /var/www;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    #domain1 setup <----------- my issues might be here 
    location /domain1 {
            #try_files $uri $uri/  /domain1/index.php?$args;
        root /var/www/;
    }

    #domain2 setup <----------- my issues might be here 
    location /domain2 {
            #try_files $uri $uri/  /domain2/index.php?$args;
        root /var/www/;
    }

}

How could I setup in docker container for nginx conf file to be able to http://domain1.com and http://domain2.com

I found most answers are using separate docker container for each site. But I need using single container for some reason.

My answer:


If you want each site to answer to different hostnames then you need to use separate server blocks for each hostname in the backend, as you already did on the other nginx server.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.