skip to Main Content

I have services running in docker-compose and i want to reverse proxy to them using nginx

How do I reach my backend service through the proxy, at the moment Nginx not passing my request on to backend. I also want to strip out the /api/search before the request reaches the backend service (meilisearch)

meilisearch    | [2022-07-11T14:00:02Z INFO  actix_server::server] Actix runtime found; starting in Actix runtime
nginx          | 172.25.0.1 - - [11/Jul/2022:14:00:09 +0000] "GET /api/search HTTP/1.1" 301 169 "-" "insomnia/2022.4.2" "-"
nginx          | 2022/07/11 14:00:09 [error] 31#31: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.25.0.1, server: _, request: "GET /api/search/ HTTP/1.1", host: "127.0.0.1"
nginx          | 172.25.0.1 - - [11/Jul/2022:14:00:09 +0000] "GET /api/search/ HTTP/1.1" 404 153 "-" "insomnia/2022.4.2" "-"

This is the reverse_proxy.conf file

server{
    listen 80;
    server_name _;
    location /api/ {
        location /api/search/ {            
            proxy_pass http://meilisearch:7700; 
            rewrite ^/api/search(/.*) $1 last;           
        } 
    }       
}

This is the docker-compose.yml file

version: '3'
services:
  nginx:
    image: nginx:1.23.0-alpine
    hostname: nginx
    container_name: nginx
    networks:
      - safedawanetwork 
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./reverse_proxy.conf:/etc/nginx/conf.d/default.conf
  meilisearch:
    image: getmeili/meilisearch:v0.28.0rc4
    hostname: meilisearch
    container_name: meilisearch
    networks:
      - safedawanetwork       
networks:
    safedawanetwork:
        driver: bridge

When I start the services with sudo docker-compose up and curl localhost/api/search I expect to see the meilisearch response but I don’t. Whats going on

2

Answers


  1. Please checkout this NGINX Pitfalls and Common Mistakes
    tra
    Based on the common mistakes, you shouldn’t use a nested location in /api location here, so based some updates, you can try this:

    server {
        listen 80;
        server_name _;
        location / {
            proxy_pass http://meilisearch:7700; 
            location /api/search/ {            
               rewrite ^/api/search(/.*) $1 last; 
            } 
        }  
    }
    

    However if you still have issues, let me know, so I can edit this answer and help you.

    Login or Signup to reply.
  2. This should be doable just using a proxy pass that ends with a slash (/).
    This works since nginx omits the matching part of the location block:

    server{
        listen 80;
        server_name _;
        location /api/ {
            location /api/search/ {     
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   Host      $http_host;
                proxy_pass http://meilisearch:7700/;          
            } 
        }       
    }
    

    A request looking like: curl localhost/api/search/test123
    will result in a request within your search service that ends up looking like this:

    GET /test123 HTTP/1.0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search