基于Nginx实现Docker代理路由:域名路径访问多容器应用
Hey there! Let's get your path-based routing set up with Nginx reverse proxy for your Docker Compose apps. Here's a step-by-step solution tailored to your scenario:
Step 1: Create the Nginx Configuration File
First, make a nginx.conf file with the reverse proxy rules. This config will route requests to example.com/app1 to your app1's 1111 port, and example.com/app2 to app2's 3333 port.
server { listen 80; server_name example.com; # Route for app1 location /app1/ { # The trailing slash here is CRUCIAL — it strips /app1/ from the forwarded path proxy_pass http://localhost:1111/; # Pass necessary headers to the backend app proxy_set_header Host $host; 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; } # Route for app2 location /app2/ { proxy_pass http://localhost:3333/; proxy_set_header Host $host; 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; } # Optional: Handle root path (redirect to app1 or show a landing page) location / { return 302 /app1/; # Uncomment below to show Nginx's default page instead # root /usr/share/nginx/html; # index index.html; } }
Step 2: Set Up Nginx with Docker Compose
Create a docker-compose.yml for the Nginx proxy to run it as a container, mounting your custom config:
version: '3.8' services: nginx-proxy: image: nginx:alpine ports: - "80:80" # Uncomment below if you need HTTPS (don't forget to add SSL certs) # - "443:443" volumes: # Mount your custom config into the container - ./nginx.conf:/etc/nginx/conf.d/default.conf # If using HTTPS, mount your SSL certificate and key here # - ./ssl/cert.pem:/etc/nginx/ssl/cert.pem # - ./ssl/key.pem:/etc/nginx/ssl/key.pem restart: always # Optional: If your apps use custom Docker networks, add Nginx to them here # networks: # - app1-network # - app2-network # Declare external networks if you added them above (they must exist from your app's compose files) # networks: # app1-network: # external: true # app2-network: # external: true
Key Things to Keep in Mind
- Trailing Slashes Matter: The
/at the end ofproxy_pass http://localhost:1111/ensures that requests likeexample.com/app1/loginget forwarded tohttp://localhost:1111/login, nothttp://localhost:1111/app1/login(which would break your app). - Network Access: If your apps don't expose ports to the host machine (using
exposeinstead ofportsin their compose files), it's better to connect Nginx to your apps' custom Docker networks. Then you can use the app service name instead oflocalhost, e.g.,proxy_pass http://app1-web:1111/(replaceapp1-webwith your actual service name from app1's compose). - App Path Compatibility: Some apps hardcode absolute paths (like
/css/style.css). In this case, you'll need to either:- Configure your app to run under a base path (e.g.,
/app1— check your app's docs for base URL settings), or - Add
sub_filterrules to Nginx to rewrite paths in the response:sub_filter '/css/' '/app1/css/'; sub_filter '/js/' '/app1/js/'; sub_filter_once off;
- Configure your app to run under a base path (e.g.,
- HTTPS Setup: To add HTTPS, create a
sslfolder with your certificate and key, then add aserverblock innginx.conflistening on 443 with SSL settings, and redirect 80 traffic to 443.
Once you've set this up, start the Nginx proxy with docker-compose up -d, and you should be able to access your apps via example.com/app1 and example.com/app2!
内容的提问来源于stack exchange,提问作者Flo
相关产品推荐
相关产品推荐

