nginx proxy_pass missing path - nginx

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;
}

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?

How to correctly change the location directive in 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?

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

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: Forward request to a sub folder under a specific port without changing URL

Is it possible to forward http://example.com to http://example.com:5000/foo?
I tried using proxy_pass to forward requests to port 5000:
location / {
proxy_pass http://127.0.0.1:5000/;
}
Which works as expected. However, if I want to forward requests to /foo, I can't just change the proxy_pass to proxy_pass http://127.0.0.1:5000/foo/ since it wouldn't be able to find the static files.
So I then tried to use try_files in hopes to serve the static files correctly:
location / {
try_files $uri /foo/;
}
location /foo/ {
proxy_pass http://127.0.0.1:5000/;
}
But that wouldn't work either since the resources are under port 5000. Any help?

Resources