在标准反向代理场景中,我使用nginx将所有请求传递给另一个主机,但是我试图使用非标准端口。
我的最终目标是将X-Forwarded-Port头设置为请求进入的端口。
以下是我在nginx.conf中的位置块:
位置/ proxy_pass http://otherhost:8090;proxy_set_header主机$host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X转发-For $proxy_add_x_forwarded_for;proxy_set_header X转发-Proto $scheme;proxy_set_header X转发-主机$host;proxy_set_header X转发-端口;}
此nginx运行在一个docker容器中,该容器被配置为将请求从8085转发到容器中的80,因此nginx进程正在侦听80:
0.0.0.0:8085->80/tcp
当我点击URL时:
http://localhost:8085/auth/
我被正确地重定向到http://otherhost:8090,但是X-Forwarded-Port头丢失或错误。
在原始块中有<VAR>的地方,我尝试了以下方法:
$server_port -这是nginx监听的端口(80),而不是请求端口。$pass_port -在我的设置中似乎是空的,所以nginx会删除标题。$http_port --这是每个请求的随机端口。$remote_port --这是每个请求的随机端口。我可以在部署时将我的配置更改为硬编码到传入请求的已知端口,但理想情况下,我可以更改前端端口而不更改nginx配置。
我已经搜索过nginx变量列表,但是找不到任何类似$request_port的东西。我有什么办法达到我的目的吗?
发布于 2020-08-11 20:44:23
我找到的唯一解决办法是使用map规则从http_host变量获得端口。
map $http_host $port {
default 80;
"~^[^\:]+:(?<p>\d+)$" $p;
}发布于 2020-08-18 09:02:53
这是编写Nginx conf的一个粗略的想法,但我相信这可以帮助您重定向。
server {
listen 80;
server_name host.docker.internal;
# By default land on localhost:80 to root so in root we copied UI build to the ngnix html dir.
# have a look to docker-compose uiapp service.
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# after location add filter, from which every endpoint starts with or comes in endpoint
# so that ngnix can capture the URL and reroute it.
# like /backend/getUserInfo/<UserId>
# In above example /backend is that filter which will be captured by Ngnix and reroute the flow.
location /backend {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_pass http://<ContainerName>:<PortNumber>;
# In our case Container name is as we setup in docker-compose `beservice` and port 8080
proxy_pass http://beservice:8080;
}
}要了解更多细节,您可以查看此项目。
发布于 2020-08-12 09:02:02
在容器内部,您无法访问外部配置。而且,nginx不支持在大多数配置块中使用环境变量。
但是,如果使用波科,您可以在一个简单的env文件中定义端口、路径等。这些定义也由docker-组合配置或env变量使用。例如
yourconfig.env
PROJECT_HOME=..
MAGIC_DOCKER_PORT=8085poco.yml文件(项目的主配置,定义计划)
version: '3.0'
maintainer: "youremail@yourdomain.com"
environment:
include: docker/conf/yourconfig.env
plan:
live:
description: "your plan to run live"
docker-compose-file:
- docker/dc-proxy.yml
- docker/dc-other-service.yml
# you can define other plans to run local/dev env在docker/dc-proxy.yml中编写文件(相关部分):
version: '3'
services:
proxy:
image: nginx:alpine
env_file: conf/yourconfig.env
ports:
- ${MAGIC_DOCKER_PORT}:80
command: /bin/sh -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" 最后,您的配置模板相关部分来自/etc/nginx/conf.d.d/mysite.Template
listen ${MAGIC_DOCKER_PORT};
...
location /auth/ {
...
proxy_set_header X-Forwarded-Port ${MAGIC_DOCKER_PORT};
...
}我使用以下文件夹结构来组织文件。这不是强制性的,但对我来说很容易使用,因为我的每个项目结构都是一样的。
/
docker <- all of docker related files here
conf <- env files, service configs, etc
scripts <- shell scripts which used by containers
dc-*.yml <- separate all services into single compose files
poco.yml <- main config, "start here" point
project-related-files <- separated by each service解释
在.env文件中定义键值对.poco用于创建坞-组合文件,并用于在nginx容器中创建环境变量。然后使用envsubst填写nginx配置模板,最后启动nginx服务。
https://stackoverflow.com/questions/60616564
复制相似问题