Reputation: 2179
I am setting up a Django project and using combination of nginx and uwsgi to serve them. Plus, I am using Docker to put django+uwsgi in one container and nginx in other.
For testing purpose, I want to set this up locally and I want to use a custom domain name as well for testing. I am really struggling to set it up.
This is my nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
#gzip on;
upstream app {
server 0.0.0.0:8000;
}
server {
listen 80;
charset utf-8;
server_name someapp.app *.someapp.app;
location / {
# checks for static file, if not found proxy to app
uwsgi_pass app;
include uwsgi_params;
#try_files $uri @proxy_to_app;
}
location /static {
alias /static;
}
location /media {
alias /media;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://someapp.app;
}
}
}
and this is my uwsgi.ini
[uwsgi]
http-socket = 0.0.0.0:8000
chdir = /app
master = true
processes = 4
cheaper = 2
chmod-socket = 664
env = DJANGO_SETTINGS_MODULE=config.settings.local
module = config.wsgi:application
vacuum = true
I am unable to connect using somapp.app in my url browser, or for that matter abc.someapp.app. Is there any other setting that I need to put in to make it work?
Upvotes: 1
Views: 1722
Reputation: 56667
To glue the containers together, you need to do a few things.
First, create a user network in Docker. This will give the containers a place to communicate and discover each other by name.
docker network create my_app
Now, when you run these containers, use that network for both.
docker run --network my_app <other-options>
This will do two things. First, it will put the containers on a network where they can communicate with each other, and not have interference from anything else Docker might be doing. Second, it will allow them to find each other using their container names, as if those names were DNS hostnames.
Now, you need to make sure each application knows how to find the other. In your case, this mostly means Nginx needs to know where to find the uwsgi server.
Your uwsgi server is already binding to 0.0.0.0:8000
. That is good, because it is binding to the external interface of the container (not only to localhost). To make this work you will need to make sure the port is exposed to Docker. In the Dockerfile where you build this, ensure you have this.
EXPOSE 8000
That will expose the port within Docker (it does not necessarily publish it to the Internet).
Now that we know where uwsgi is, and that its port is exposed, you can tell Nginx where to find it. You are currently doing this, which won't work:
upstream app {
server 0.0.0.0:8000;
}
You need to modify this to use the other container's name instead.
upstream app {
server uwsgi:8000;
}
Since you want to connect to Nginx from outside, in addition to exposing the port,
EXPOSE 80
You will need to publish it. Something like this.
docker run --network my_app -p 80:80
Or if the bound port should be something other than 80 on the outside host, you can use a different one.
docker run --network my_app -p 8080:80
Last is the piece where you want to use a custom hostname. This is less easy, because by default Docker will not handle this for you. If you're running this locally, such as on a laptop, it's easy: just use localhost, possibly with a specific port.
http://localhost:8080
If you want to reach this from some other place, remotely, you may need to add an IP to the /etc/hosts
of the client system, or add something to DNS. It's hard to say more about that without knowing what environment you are actually in.
Upvotes: 4