How to correctly change the location directive in nginx? - nginx

I implemented a simple REST API using Falcon and running it with Gunicorn. An example call looks like this
curl "http://localhost:5000/articles?limit=100"
Now I'm trying to make the API accessible using nginx, and I actually got it working using the following config file for nginx
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
location / {
include proxy_params;
proxy_pass http://localhost:5000;
}
}
With this I can go to http://xxx.xxx.xxx.xxx/articles?limit=100 to get the respective response. The only thing I would like to do now is to change the location directive. When I change the config file to
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
location /test/ {
include proxy_params;
proxy_pass http://localhost:5000;
}
}
I would assume that http://xxx.xxx.xxx.xxx/test/articles?limit=100 will again give me the correct response, but I get an 404. Using /test instead of /test/ doesn't help either. What am I missing?

Related

Nginx proxy_pass with unlimited chars after custom location

I dont understand how to get proxy_pass working on any location path after /api/some/path/more/etc
Right now i have this conf:
server {
listen 80;
location / {
root /usr/share/nginx/html/;
index index.html index.htm;
}
location /api/ {
proxy_pass http://local-api-server:5005/;
}
}
this conf works only if i browse http://localhost/api i get served api response, alright, thats good, but when i make request to http://localhost/api/some/another/thing it gets denied.
I dont want to hardcode every possible location in the conf file. how do i mend this?

nginx not finding css file

I have just put my flask app on ubuntu. I am using gunicorn and nginx. The app is loading and nginx can find the templates. However the CSS files are not being served. If I enter the URL to access the CSS file in the browser I get a 403.
Here is my config file
server {
listen 80;
server_name myip;
location /static {
alias /home/ubuntu/enrich/website/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
Any idea where I am going wrong?
Thx
S

How to proxy to another URL without redirect

Is it possible to use nginx to work like this:
When going to x.com/blog -> display the content of blog.x.com (with all the query parameters, etc) without changing the browser URL (without browser redirecting)
I tried
location /blog {
proxy_pass https://blog.x.com
}
but that didn't work
This nginx config works for me.
server {
listen 80;
listen [::]:80;
server_name x.com;
location /blog {
proxy_pass https://blog.x.com;
proxy_set_header Host blog.x.com;
}
}

Nginx automatically add trailing slash before query string

When I connect to:
http://localhost/api?foo=bar
Nginx redirect me to:
http://localhost/api/?foo=bar
Therefore, my backed do not respond correctly.
I have tried change location to location /api then problem solved but it also match something like http://localhost/apiapi?foo=bar and I don't want that.
Here is my config:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}
You want to use the URI /api to access your service.
The location /api/ does not match that API unless an additional / is added (which Nginx is automatically appending via a redirect).
The location /api also matches any URI that begins with the same three characters.
As your service only needs to respond to the single URI, you can use an exact match location.
For example:
location = /api {
...
}
See this document for details.

nginx proxy_pass missing path

So I am running some nginx and jenkins in docker containers (same machine).
I have setup a proxy_pass in nginx as follows
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /jenkins/ {
proxy_pass http://infrastructure_jenkins_1:8080/;
}
}
The redirect goes to http://54.194.42.13/static/b703e301/css/layout-common.css which results in a 404.
How do i configure the proxy to include /jenkins in the path i.e. http://54.194.42.13/jenkins/static/b703e301/css/layout-common.css ?
Remove the trailing / from the proxy_pass statement. The trailing / is instructing nginx to substitute the value of the location statement (/jenkins/) with /. See this document for more.
For example, to reverse proxy without altering the original URI use:
location /jenkins/ {
proxy_pass http://infrastructure_jenkins_1:8080;
}

Resources