当我最后将端口 3000 添加到映射到 namecheap 上托管的域名的弹性 Ip 地址时,我只能访问托管在 ec2 实例上的网站。
我的泊坞窗包含;前端、后端、nginx 和 certbot。
当我构建 docker 项目时,一切看起来都正常,没有任何警告。
这是我的 docker compose yml 文件和 .env 文件的视图:
1-docker 撰写 yml:
version: '3'
services:
# Client container
client:
build:
context: frontend
dockerfile: Dockerfile
container_name: client
ports:
- "80:3000"
volumes:
- ./frontend:/var/www/frontend
#Laravel App
app:
build:
context: backend
dockerfile: Dockerfile
image: hakase-labs/laravel
container_name: app
restart: unless-stopped
tty: true
ports:
- "80:9000"
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/backend
volumes:
- ./backend/php/local.ini:/usr/local/etc/php/conf.d/local.ini
- ./backend:/var/www/backend
depends_on:
- db
#SSL
certbot:
image: certbot/certbot:latest
container_name: certbot
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
#Nginx Service
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
# Nuxt port
- "80:80"
# Laravel port
- "81:81"
- "443:443"
volumes:
- ./:/var/www
- ./certbot/www:/var/www/certbot/:ro
- ./certbot/conf:/etc/letsencrypt/:ro
- ./nginx/config/default.conf:/etc/nginx/conf.d/default.conf
- ./nginx/config/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- app
- client
#MySQL Service
db:
image: mysql:5.7
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: *******
MYSQL_USER: **************
MYSQL_PASSWORD: **************
MYSQL_ROOT_PASSWORD: *************
volumes:
- mysqldata:/var/lib/mysql/
#Docker Networks
# networks:
# mynet:
# driver: bridge
#Volumes
volumes:
mysqldata:
driver: local
2-nginx 默认配置文件:
#--------------------------------------------------------
# Nuxt.JS server configuration
#--------------------------------------------------------
proxy_cache_path /tmp/cacheapi levels=1:2 keys_zone=microcacheapi:100m max_size=1g inactive=1d use_temp_path=off;
map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
default off;
}
server {
listen 80;
listen [::]:80;
access_log off;
error_log off;
server_name mydomain.com;
location ~ /.well-known/acme-challenge {
root /var/www/certbot;
}
return 301 https://$server_name$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
access_log off;
server_name mydomain.com;
#ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
location ~ /.well-known/acme-challenge {
root /var/www/certbot;
}
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location / {
# Proxy to Node.JS instance
proxy_pass http://client:3000;
expires $expires;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
# Websocket support
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 ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
server {
listen 80;
listen [::]:80;
access_log off;
error_log off;
server_name admin.mydomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
return 301 https://$server_name$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name admin.mydomain.com;
access_log off;
# Micro caching
proxy_cache microcacheapi;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
proxy_cache_background_update on;
proxy_cache_lock on;
#ssl_certificate /etc/letsencrypt/live/admin.mydomain.com/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/admin.mydomainy.com/privkey.pem;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
index index.php index.html;
root /var/www/backend/public;
charset utf-8;
client_max_body_size 20m;
location / {
try_files $uri $uri/ /index.php?$args;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
# Handle all php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
# For comfortable debugging
fastcgi_read_timeout 1000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
PS;我的 ec2 实例的安全组内允许使用端口 80 和 443。 我在入站规则中添加了 3000 端口作为 TCP,当我将此 3000 端口添加到我的网站时,我可以访问它。
我想要的是使用默认的http端口80访问我的网站。
谢谢
我尝试将 docker compose yml 文件中的端口更改为 80,但是当我构建 docker 映像时,它说端口 80 已分配
您可以执行一次 docker ps 并检查您的 docker 容器所遵循的实际端口映射是什么吗?这将有助于排除故障。