Sathya
Sathya

Reputation: 31

nginx proxy_pass for get request api call with parameters

I have simple flask rest api that fetches data from database and gives result to user in json file. flask server gets parameters from html input fileds upon submit and these parameters are used to fetch data from database. It is working fine with flask inbuilt WSGI server. I wanted to deploy in production with nginx as my web server and gunicorn as application server. When I run docker container and access the root url, i can get html form for the user to input parameters. When I click on submit, another api resource call gets invoked, but I get either 'URL not found' or 'internal server error'. This surely problem with nginx location configuration for my get request api call with parameters in URL. Please help me how to configure nginx proxy_pass URL for this kind of request.

My browser request URL look something like this when I submit form. http://IP address/api/v1/service?key=12345&name=abc&id=1234

HTML (form.html)

<form  name="device" action="/api/v1/service">

Python view functions

@app.route('/')
def my_form():
    return render_template('form.html')
@app.route('/api/v1/service', methods=['GET'])
def my_form_get():
    ..............
    .............

nginx server

server {
    listen      80;

    location / {
        proxy_pass http://localhost:5000/;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
   location  /api/v1/service {
        proxy_pass http://localhost:5000/api/v1/service;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Gunicorn configuration

[program:gunicorn]
command=/usr/bin/gunicorn run:app -b localhost:5000
directory=/deploy/app

Upvotes: 3

Views: 4113

Answers (1)

Alex Elkin
Alex Elkin

Reputation: 624

You can use the following nginx configuration:

upstream v1service {
  server v1servicecontainer:8080;
}

server {
  listen 80;

  location ~ ^/api/v1/service(.*)$ {
  resolver 127.0.0.1;
  proxy_pass http://v1service/api/$1$is_args$args;
  proxy_redirect off;
  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-Host $server_name;
}

Upvotes: 2

Related Questions