背景- Web应用程序
我在8090端口的Ubuntu主机上运行了一个容器化的应用程序。下面是连接后端、Postgres服务器和Vue+Nginx前端的坞撰写文件:
version: "3.8"
services:
# DATABASE BACKEND
use_db:
container_name: use_db
image: postgres:14.2
expose:
- "5433"
ports:
- "5433:5433"
environment:
# POSTGRES_HOST_AUTH_METHOD: "trust"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "blabla"
POSTGRES_DB: "use_db"
command: "-p 5433"
restart: always
volumes:
- db:/var/lib/postgresql/data
# FRONT END (LOOKING TO INTERNET)
use_frontend:
container_name: 'use_frontend'
build:
context: ./admin
dockerfile: Dockerfile
restart: always
depends_on:
- use_backend
ports:
- 8090:80 # port forwarding = HOST:DOCKER
# BACKEND (FASTAPI)
use_backend:
container_name: 'use_backend'
build:
context: ./api
dockerfile: Dockerfile
restart: always
depends_on:
- use_db
environment:
DATABASE_URL: "postgres://....."
HOST_LOCATION: "http://<HOST IP>:8090"
command: gunicorn --bind 0.0.0.0:8000 -k uvicorn.workers.UvicornWorker main:app
volumes:
db:
driver: local因此,当码头容器用docker compose up -d启动时,我可以在<HOST>:8090上访问web应用程序。
在前端容器中,Nginx conf看起来如下所示:
events {}
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
client_max_body_size 20M;
location / {
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://use_backend:8000/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
}
location /ws/ {
proxy_pass http://use_backend:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
}
}
}目标
现在我的下一个目标是通过一个普通的URL访问web应用程序。主机有一个与其一个用户帐户相关联的付费域名,我们将其命名为example.com。因此,在/home/example.com/中有一个虚拟的/home/example.com/,可以用一个真正的web应用程序代替它,从互联网上作为https://example.com访问它。
还有一个Nginx服务器直接运行在主机上,其配置位于/etc/nginx/nginx.conf中,如下所示:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 512M;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POOD LE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
}当我检查包含80 (lsof -n -i -P | grep 80)的开放端口时,我得到:
nginx 167921 root 11u IPv4 1381601 0t0 TCP *:80 (LISTEN)
nginx 167922 www-data 11u IPv4 1381601 0t0 TCP *:80 (LISTEN)
nginx 167923 www-data 11u IPv4 1381601 0t0 TCP *:80 (LISTEN)
nginx 167924 www-data 11u IPv4 1381601 0t0 TCP *:80 (LISTEN)
nginx 167925 www-data 11u IPv4 1381601 0t0 TCP *:80 (LISTEN)它确认Nginx服务正在主机上运行,侦听端口80。
大问题
如何将我的停靠程序(在端口
8090上运行)绑定到主机域example.com(在默认端口HTTP80/ HTTPS8080上运行),以便从https://example.com
访问该应用程序
发布于 2022-10-04 22:51:00
您可以:
nginx,并在主机端口80和443上发布Docker容器:港口:- 80:80 - 443:443
这假定您的Docker应用程序在端口443上已经有一个TLS侦听器(从您的问题中可以看出它是否存在)。
位置/{ proxy_pass https://localhost:8090/;}
在本例中,您可以将nginx配置为侦听端口443上的TLS连接(例如使用these instructions),并为您的http和https侦听器配置一个代理节。
https://stackoverflow.com/questions/73954120
复制相似问题