docker 我的 nginx 配置有什么问题?
What's wrong with my nginx configuration with docker?
我有一个包含 3 个容器的 docker 环境:前端 (angular)、后端 (dotnet ) 和 nginx.
我正在尝试使用 proxy_pass 配置 nginx 以将 /api 位置指向我的 API 来自其中一个容器。
这是我的nginx配置:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
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 /api {
proxy_pass http://sitr-api:80;
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;
}
}
这些是我的 Dockerfiles:
API 网络:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY . .
EXPOSE 80
ENTRYPOINT ["dotnet", "SITR.Web.Host.dll", "--environment=Staging"]
前端 angular:
FROM nginx
COPY . /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d
EXPOSE 80
nginx
FROM nginx
COPY default_nginx.conf /etc/nginx/conf.d/default.conf
我的docker-撰写文件
version: '3.0'
services:
sitr-api:
image: sitr-api
container_name: sitr-api
environment:
ASPNETCORE_ENVIRONMENT: Staging
ports:
- "9901:80"
volumes:
- "./Host-Logs:/app/App_Data/Logs"
sitr-app:
image: sitr-app
container_name: sitr-app
ports:
- "9902:80"
nginx:
image: sitr-nginx
container_name: sitr-nginx
depends_on:
- sitr-app
- sitr-api
ports:
- "81:80"
容器正在运行,因为我能够访问localhost:9901(后端)和localhost:9902(前端)。
我在 localhost:81 上通过 nginx 访问的前端也在工作,但是 proxy_pass 到我的 localhost:81/api后端不工作(http 404)。
我的 nginx 配置有什么问题?
我根据@super 的评论发布回复。
根据他的评论,当他重定向时,只需要做一个 rewrite 来删除 /api。
我需要用正则表达式添加以删除 /api 的具体行是:
rewrite ^/api(/.*)$ break;
这是完整的 nginx 配置:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
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 /api {
rewrite ^/api(/.*)$ break;
proxy_pass http://sitr-api:80;
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;
}
}
我有一个包含 3 个容器的 docker 环境:前端 (angular)、后端 (dotnet ) 和 nginx.
我正在尝试使用 proxy_pass 配置 nginx 以将 /api 位置指向我的 API 来自其中一个容器。
这是我的nginx配置:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
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 /api {
proxy_pass http://sitr-api:80;
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;
}
}
这些是我的 Dockerfiles:
API 网络:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY . .
EXPOSE 80
ENTRYPOINT ["dotnet", "SITR.Web.Host.dll", "--environment=Staging"]
前端 angular:
FROM nginx
COPY . /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d
EXPOSE 80
nginx
FROM nginx
COPY default_nginx.conf /etc/nginx/conf.d/default.conf
我的docker-撰写文件
version: '3.0'
services:
sitr-api:
image: sitr-api
container_name: sitr-api
environment:
ASPNETCORE_ENVIRONMENT: Staging
ports:
- "9901:80"
volumes:
- "./Host-Logs:/app/App_Data/Logs"
sitr-app:
image: sitr-app
container_name: sitr-app
ports:
- "9902:80"
nginx:
image: sitr-nginx
container_name: sitr-nginx
depends_on:
- sitr-app
- sitr-api
ports:
- "81:80"
容器正在运行,因为我能够访问localhost:9901(后端)和localhost:9902(前端)。
我在 localhost:81 上通过 nginx 访问的前端也在工作,但是 proxy_pass 到我的 localhost:81/api后端不工作(http 404)。
我的 nginx 配置有什么问题?
我根据@super 的评论发布回复。 根据他的评论,当他重定向时,只需要做一个 rewrite 来删除 /api。
我需要用正则表达式添加以删除 /api 的具体行是:
rewrite ^/api(/.*)$ break;
这是完整的 nginx 配置:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
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 /api {
rewrite ^/api(/.*)$ break;
proxy_pass http://sitr-api:80;
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;
}
}