简体   繁体   中英

Setting up subdomains with nginx proxy manager

I'm trying to set up a simple hello world flask app managed as a docker container behind nginx proxy manager (NPM) and accessed through a subdomain.

I have portainer and NPM working, I exposed port 80 and 443 on my router, and I've set up my cloudflare domain to point to my pi with ddclient to manage changing IPs. So far when I go to the domain I can see the npm greeting page without a problem.

来自 cloudflare 的工作域

Now, what I would like to have happen is when someone navigates to the subdomain, in this case helloflask.mydomain.cloud. They will be pointed to an nginx/docker compose app. However nothing has worked yet, and all I see are 502 bad gateways or "The page isn't redirecting properly" when I try to access it from the helloflask.mydomain.cloud url, and I'm not sure why or what to check to fix it.

页面未正确重定向

I can connect to the server locally through port 8001 just fine.

Here are my current docker compose settings for the nginx and flask containers.

version: "3.7"

services:

  flask:
    build: ./flask
    container_name: hello_world_flask
    restart: always
    environment:
      - APP_NAME=MyFlaskApp
    expose:
      - 8080

  nginx:
    build: ./nginx
    container_name: hello_world_flask_nginx
    restart: always
    ports:
      - 8001:80

And my docker containers , hello_world_flask_default is the docker network with the nginx and flask containers. So I put that in my NPM Proxy Host , with details and SSL settings . (Maybe it's an https issue?)

And here are my cloudflare domain settings.

Thanks!

This is what you need to:

Make sure your nginx proxy manager and your docker container runs on the same network.

  1. put http inside the scheme textbox
  2. put the service name or get the IP address of the container from portainer under "Forward hostname / IP"
  3. Put the port number in your case it's 80
  4. domain name has to be your sub-domain like helloflask.mydomain.cloud

That should work

You can put the network into the docker-compose something like this

version: "3.7"

services:

 flask:
  build: ./flask
  container_name: hello_world_flask
  restart: always
  environment:
   - APP_NAME=MyFlaskApp
  expose:
   - 8080

 nginx:
  build: ./nginx
  container_name: hello_world_flask_nginx
  restart: always

networks:
 default:
  external:
   name: proxy

Make sure you format the docker-compose.yml file properly and make sure the nginx proxy manager and your flash app ngnix containers all belong to the same network "proxy" is just a contrived example for naming a reverse proxy but I think you got the point.

make sure to get the right IP address of your nginx container by running

docker inspect your-flask-app-nginx-container-name/id

then you will get a screen like this where you can grab the right IP address of that container

docker 容器的 IP 地址

Make sure to check the all the containers network and the nginx reverse proxy containers network are the same

then add a proxy using nginx reverse proxy like this

添加代理主机

You can add the SSL settings on the SSL tab yourself:) also make sure to put the right IP address and don't copy from the screenshot because it won't work if you don't put the right IP address. I think that should work because I don't have any problem on my server, I'm using the same setup, let me know, good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM