在Nginx上,服务请求到自定义端口

分享于 

2分钟阅读

互联网

  繁體

问题:

我在使用docker-compose,这是docker-compose.yml:


version: '3.3'



services:



 nginx:


 image: nginx:latest


 build: .


 container_name: nginx


 depends_on:


 - service-1


 ports:


 - "80:80"



 service-1:


 image: dockerhub/image:latest


 container_name: service-1


 ports:


 - "8080:8080"


 restart: on-failure



用于生成dockerfile Nginx的包含下列内容:


FROM nginx:latest


COPY ./nginx-html-template/ /usr/share/nginx/html/


COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf


EXPOSE 80


CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]



nginx.config:


server {


 listen [::]:80;


 listen 80 default_server;


 server_name localhost;



 location / {


 root /usr/share/nginx/html;


 index index.html index.htm;


 try_files $uri $uri/ =404;


 }



 location /service-1 {


 proxy_pass http://service-1:8080/;


 }


}



如果我访问localhost/service-1,我希望nginx将请求重定向到端口8080。


答案1:

尝试像这样修改它:


 location /service-1/ {


 proxy_pass http://127.0.0.1:8080/;


 }





相关文章