Nginx location proxy pass homepage one place, all other routes, another place - nginx

I need your help, I have not been able to find the exact way to do this.
I would like to send my homepage ONLY to one proxypass location
location / {
proxy_pass http://website;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
And then send every other route (I won't know what they will all be, it will be dynamic) to another proxy pass.
location /* {
proxy_pass http://interaction;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
I know the * is wrong (since it doesn't work), but it was just to represent /anything or /whatever or /idontknowyet
Thanks for the help!

You are not clear as to which URIs constitute your homepage (other than /).
Assuming that no resource files are required, and no redirection occurs, an exact match location block can be used to match the homepage, using the default location block to match everything else:
location = / {
proxy_pass http://website;
...
}
location / {
proxy_pass http://interaction;
...
}
See this document for details.
But there are probably other URIs (such as css and js files) that make up the homepage. You will need to add location blocks for those too, or perhaps use a regular expression location block that matches all of the URIs required by the homepage.

Related

NGINX as reverse proxy to serve paths directly via url

I am looking to configure nginx to serve following requirement
We are serving individual websites at www.company.com/mywebsites and want to use nginx as reverse proxy to serve the same website on custom URL e.g www.mywebsites.com
I have configured dns of mywebsites.com to point to nginx server and following code block serves the home page of www.company.com
server {
server_name www.mywebsites.com;
location / {
proxy_pass http://127.0.0.1:9002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
however i am struggling to get the part of redirecting www.mywebsites.com to the www.company.com/mywebsites path. I have tried added proxy_redirect but that didn't help and also trying to add path in the proxy_pass doesn't work either as it then tries to fetch all of the components using www.company.com/mywebsites/required/componenets.css which results in 404 as the correct path is www.company.com/required/componenets.css
You might need to do this change:
server {
server_name www.mywebsites.com;
location / {
rewrite /mywebsites/(.*) /$1 break;
proxy_pass http://127.0.0.1:9002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
However, I truly recommend that this issue to be fixed from the site code itself (I don't know what the site is developed with), as this might have unknown side effects on some URLs.

Nginx redirect with parts of URL

I have a redirect problem with nginx and I really don't know how to do it.
The problem is that I need to redirect all the traffic to a website INCLUDING THE PATH
BASE_URL: https://example.com/auth/
For example if the request is: https://example.com/auth /some/path/
I need it to redirect to: https://newdomain.com /some/path/
In other words, I need to concatenate the part of / some / path / to the new URL.
Here is the part of my code that I have tried
location /auth/* {
proxy_pass https://newdomain.com/$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
Thanks.
the issue seems to be the URL handling. You need a small regexp and get the result.
try:
location ~ ^/auth/(.*)$ {
proxy_pass https://newdomain.com/$1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
You can find examples there https://www.liaohuqiu.net/posts/nginx-proxy-pass/

NGINX internal redirect from uri path to JSF context root

I'm configuring a cloud server which use NGINX as reverse proxy to serve different applications on different URI (all the applications are on the same wildfly standalone instance).
To be more specific i've a JSF application with a contextroot, let's say, /jsfcontext and i've set up a NGINX location like /mypublicuri.
What happens is that when I navigate to https://myserver.com/mypublicuri/index.xhtml i receive the following error:
/mypublicuri/index.xhtml Not Found in ExternalContext as a Resource.
I'm pretty sure it's related to a missing internal redirect route or some kind of "hack" that i need to specify in order to make everything work but i'm a newbie in NGINX and I don't know how to properly set everything up.
Thanks for the help
Cheers
Read NGINX documentation but my lack of english knowledge makes difficoult to understand what should I have to do
My actual NGINX config
server {
server_name myserver.com www.myserver.com;
access_log /usr/share/logs/access.log;
error_log /usr/share/logs/error.log;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on;
location /anotherworkingapp {
add_header Allow "GET, POST, HEAD, PUT, DELETE" always;
proxy_pass http://127.0.0.1:8080$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /mypublicuri {
proxy_pass http://127.0.0.1:8080/jsfcontext$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}

Can not proxy assets requests to file storage with nginx

I am trying to proxy requests to digital-ocean space storage, example: GET http://example.com/download/image_id.jpg ---> https://bucket-name.fra1.cdn.digitaloceanspaces.com/images/image_id.jpg, but I get
<Error>
<Code>NoSuchBucket</Code>
<BucketName>example.com</BucketName>
<RequestId>tx000000000000018d441fd-005d582ff2-1b7a15-fra1a</RequestId>
<HostId>1b7a15-fra1a-fra1</HostId>
</Error>
Here is my full configuration:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /download/ {
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
resolver 8.8.8.8;
}
}
I have already tried different options with/without trailing slash, but after reading docs carefully, I think now it is optimal, configuration,
also I tried to debug where I am proxied replacing
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images$request_uri;
with
proxy_pass https://google.com/images$request_uri;
and it gave me what I expected.
According to docs, what I want to do looks like this:
location /download/ {
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images;
...
So that download part is replaced with images, but, it didn't work.
At the moment (after debugging with google) I am almost confident that url after proxie_pass is equal to https://bucket_name.fra1.digitaloceanspaces.com/images/image_id.jpg, so maybe the key problem is in wrong approach to "spaces"
P.S. I'm open to advices about better way to log/debug proxy_pass value .
So... proxy_set_header Host $host;was breaking everything. Removing it fixed proxing.

Nginx Reverse Proxy & Rewrite with Parameters

I'm trying to deploy a Bokeh Server application that requires an input parameter ("Part") in the form:
(1) http://127.0.0.1:5100/myapp?Part=1234-567
I want to use rewrite so that when a user hits the below URL
(2) http://<my_hostname>/myappRenamed/Part=1234-567
Nginx reverse proxies to URL (1)
Right now, my config file is similar to below.
location /myappRenamed/ {
proxy_pass http://127.0.0.1:5100;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_buffering off;
}
This works when I hit
http://<my_hostname>/myappRenamed/myapp?Part=1234-567
Except I want the URL to be
http://<my_hostname>/myappRenamed/Part=1234-567
You should rewrite the URL first, making it URL friendly using nginx rewrite capabilities and the proxy it:
location /myappRenamed/ {
rewrite ^/myapp(.*) /$1 break;
proxy_pass http://127.0.0.1:5100;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_buffering off;
}
I do not have your ENV so I can't really test the rule but you got the concept.

Resources