version: '3'
services:
nginx:
image: nginx:stable
container_name: nginx
restart: always
volumes:
- ./logs:/var/log/nginx # folder to write logs in
- ./sites-enabled:/etc/nginx/conf.d:ro # all enabled sites .conf files
- ./certbot/conf:/etc/letsencrypt:ro # configuration files by certbot (keys)
- ./certbot/www:/var/www/certbot:ro # webroot for certbot check
ports: # open ports on host
- 80:80
- 443:443
networks: # attach container to an internal network
- web
networks:
web:
external: true # web network gets declared outside of this file and should not be created with docker compose up
version: '3'
services:
rest_api:
image: myimage
container_name: rest_api
expose: # expose only exposes the port to internal networks, ports would expose the ports to the host
- 8000 # exposes port 8000 to internal connected networks
networks:
- web
networks:
web:
external: true
server {
listen 80;
server_name mydomain.com; # your domain name
location / {
proxy_pass http://rest_api:8000; # needs to be http://<container_name>:<port_number>
proxy_set_header Host $host; # all headers are optional, but help the applications getting information about the original request
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ /.well-known/acme-challenge/ { # this part would be for certbot certificate requests
root /var/www/certbot;
}
}
1条答案
按热度按时间pbpqsu0x1#
所以你有你的docker容器,你的REST API运行在一个端口上,这个端口暴露在容器之外,我以
8000
为例,rest_api
为docker容器名。因为你已经在使用docker了,我建议你也用docker来设置nginx。首先,您需要为容器创建一个内部网络来进行通信。
docker network create web
应该做这个工作。然后你需要一个类似于下面的docker-compose.yml
来设置nginx:字符串
现在,您需要启动
rest_api
容器。重要的是,你也要将容器添加到Web网络中,这样它就可以被nginx容器找到。将端口暴露给内部网络而不是主机也很重要。例如docker-compose.yml
文件:型
然后你只需要配置你的nginx设置。因此,您在
sites-enabled
目录中创建了一个mysite.conf
文件。该文件可能看起来像下面这样:型
这是快速但详细的演练。让我知道如果它帮助或你有更多的问题。