I'm a bit stuck on how to accomplish the setup I want. Putting aside whether it's a good idea, I am trying to setup a development/test and production django application on the same server under the same domain. I am using nginx as the public-facing server, which then should send requests to the appropriate application server based on whether the url starts with the /dev/
prefix. However, I cannot get the nginx configuration quite right.
Ideally, requests to http://example.com/admin
go to the production app, and http://example.com/dev/admin
goes to the dev version. This way, the django urls.py
is the same for both, such as:
urlpatterns = [
path('admin/', admin.site.urls),
... other paths ...
]
The django apps are in docker containers, which communicate with the host machine via unix sockets. That is, I start the docker container with something like:
docker run -v /www:/host_dir me/mydocker
and in that container, the entrypoint command is:
gunicorn myapp.wsgi:application --bind=unix:/host_dir/xyz.sock
My nginx conf on the host machine looks like:
server {
listen 80;
listen [::]:80;
server_name 35.231.70.176;
location /dev {
include proxy_params;
proxy_pass http://unix:/www/dev.sock:;
}
location / {
include proxy_params;
proxy_pass http://unix:/www/production.sock:;
}
}
In this configuration, I get the following:
http://example.com/admin
works as expected
http://example.com/dev/admin
goes to the PRODUCTION admin site.
In addition to the location block above, I have tried several variants in the nginx conf (keeping the location / {...}
block the same as above), including:
Add slash to location:
location /dev/ { include proxy_params; proxy_pass http://unix:/www/dev.sock:; }
Add slash to proxy_pass:
location /dev { include proxy_params; proxy_pass http://unix:/www/dev.sock:/; }
Add slash to both location and proxy pass:
location /dev/ { include proxy_params; proxy_pass http://unix:/www/dev.sock:/; }
ALL of these variants end up sending http://example.com/dev/admin
to the production application, so I'm confused. I thought that since it first matches the /dev
block, it should send the request to the dev application, but clearly I'm not understanding.