我正在嘗試將域名指向我的 docker 容器。在我們的路由器中,我們將埠 80 轉發到192.168.1.101(服務器 Docker 正在運行)
然而,容器 IP 地址顯示為
"nextjs-docker-pm2-nginx-master-nextjs-1:172.18.0.2/16"
"nextjs-docker-pm2-nginx-master-nginx-1:172.18.0.4/16"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cabb3c8fe03c nextjs-docker-pm2-nginx-master_nginx "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nextjs-docker-pm2-nginx-master-nginx-1
ed27e0fd2f24 nextjs-docker-pm2-nginx-master_nextjs "docker-entrypoint.s…" 16 minutes ago Up 16 minutes 3000/tcp nextjs-docker-pm2-nginx-master-nextjs-1
我的 default.conf 是
# Cache zone
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;
upstream nextjs {
server nextjs:3000;
}
server {
listen 80;
server_name local.DOMAIN.com.au;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css application/javascript image/svg xml;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# BUILT ASSETS (E.G. JS BUNDLES)
# Browser cache - max cache headers from Next.js as build id in url
# Server cache - valid forever (cleared after cache "inactive" period)
location /_next/static {
proxy_cache STATIC;
proxy_pass http://nextjs;
}
# STATIC ASSETS (E.G. IMAGES)
# Browser cache - "no-cache" headers from Next.js as no build id in url
# Server cache - refresh regularly in case of changes
location /static {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://nextjs;
}
# DYNAMIC ASSETS - NO CACHE
location / {
proxy_pass http://nextjs;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 443 default_server ssl http2;
server_name local.DOMAIN.com.au;
ssl_certificate /etc/nginx/ssl/live/local.domain.com.au/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/local.domain.com.au/privkey.pem;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# BUILT ASSETS (E.G. JS BUNDLES)
# Browser cache - max cache headers from Next.js as build id in url
# Server cache - valid forever (cleared after cache "inactive" period)
location /_next/static {
proxy_cache STATIC;
proxy_pass http://nextjs;
}
# STATIC ASSETS (E.G. IMAGES)
# Browser cache - "no-cache" headers from Next.js as no build id in url
# Server cache - refresh regularly in case of changes
location /static {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://nextjs;
}
# DYNAMIC ASSETS - NO CACHE
location / {
proxy_pass http://nextjs;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
}
}
我們的 docker-compose 檔案是
version: '3'
services:
nextjs:
build: ./DRN1Git
nginx:
user: $UID
build: ./nginx
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./certbot/www:/var/www/certbot/:rw
- ./certbot/conf/:/etc/nginx/ssl/:ro
certbot:
image: certbot/certbot:latest
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
uj5u.com熱心網友回復:
默認情況下,Docker 創建一個內部網路。(172.18.0.0/16在你的情況下)。您需要將容器的埠映射到您的 docker 主機 ( 192.168.1.101) 的外部。請參閱compose 中的埠以供參考。例如:
version: "3.9"
services:
web:
build: nginx
ports:
- "80:80"
如果您提供您的docker-compose我將適合示例以滿足您的需求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/528552.html
