Reputation: 965
Hi I have a machine (server) and nginx(native) installed on this server.
My domain is hotels.com(for example)
And subdomain api.hotels.com
this is the conf file for subdomain on machine (native nginx):
server {
listen 80;
server_name api.hotels.com;
location / {
proxy_pass http://127.0.0.1:3000$request_uri;;
}
}
I build my app with microservices architecture, 2 services: users-service:3030 , payment-service:3080
i want to expose api.hotels.com/users/ AND api.hotels.com/payment
so i choose to use nginx(docker container) as apiGateway on port:3000 and i want to proxy to specific service, so use this config
server {
listen 3000;
location /users/ {
proxy_pass http://users:3030/;
}
location /payment/ {
proxy_pass http://users:3080/;
}
}
if i try to get http://api.hotels.com i get nginx from nginx(machine): Good
but if try to get http://api.hotels.com/users it rewrite the url in browser to http://127.0.0.1/users and then break.
*Note: i use docker-compose to up this 2 services and nginx container.
Questions:
1) how to fix this? 2) my flow is ok, or have any other flow for this app?
My purpose flow example:
1) Client http://api.hotels.com/users
2) Server all api.hotels.com ==> http://localhost:3000(api gateway)
3) Nginx Container(on port:3000) ==> proxy to spesific service by name (users in this example)
Upvotes: 2
Views: 3544
Reputation: 1404
Your application looks good, the problem is only configuration. you can try this configuration:
server {
server_name api.hotels.com;
listen 80;
location /users/ {
proxy_pass http://users:3030/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host
}
location /payment/ {
proxy_pass http://users:3080/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host
}
}
in this case your nginx container expose ports => INCOMING_PORT:80
then in your example it will look like this in docker-compose.yml
...
services:
api-gateway:
image: nginx:latest
ports:
- "3000:80"
...
in this way your api gateway is more dynamically. you can change the income port any time and nginx inside container will listen for port 80, and will proxy for spesific service by name.
good luck :)
Upvotes: 1