Changing the static root based on the host in Nginx - nginx

Is it possible to change the /static/ folder based on the host?
What I want is this:
server {
server_name code-server.domain.com;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
if ($host = code-server.domain.com/proxy/8000) {
location /static/ {
root /home/...
}
}
Is this possible?
I am using code-server, they have a proxy built in. But would I would like to see is when visiting code-server.domain.com/proxy/8000, a different static root loads. (for a development server)
Any help is appreciated!

Related

Ngixn rewrite and reverse proxy to docker container

I am running 3 services using docker compose on 3 ports: svc1:8081, svc2:8082, and svc3:8083 respectively. I have nginx installed on my host machine (not as a docker container), I want to reverse proxy all the requests to appropriate services so I am rewriting the url inside the nginx location block and performing a reverse proxy.
I am unable to get the results as something the files are not loading (mainly the static file to docker container)
My nginx config is as follows:
server {
listen 80;
server_name - ;
location /svc1/ {
rewrite ^/svc1(.*)$ $1 break;
proxy_pass http://localhost:8081;
}
location /svc2/ {
rewrite ^/svc2(.*)$ $1 break;
proxy_pass http://localhost:8082;
}
location /svc3/ {
rewrite ^/svc3(.*)$ $1 break;
proxy_pass http://localhost:8083;
}
}
I would be thankful if anyone can point if I am doing any wrong. Thanks in advance for your help.
Try this:
server {
listen 80;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Referer $http_referer;
# Additionally those headers if websocket/reload is used:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
location /svc1/ {
proxy_pass http://localhost:8081/;
}
location /svc2/ {
proxy_pass http://localhost:8082/;
}
location /svc3/ {
proxy_pass http://localhost:8083/;
}
}
Don't forget to restart nginx after changing conf!

Nginx: create a location to redirect to another location

I'm trying to redirect a location to another location, but for some reason it doesn't work. My nginx service is running under a Docker container on 8080:80 port.
I have /portal that shows my web application correctly and I would like nginx redirects from / to /portal. I've tried some approaches using location / or location = /, but with no success.
The /portal can be accessed normally by
http://localhost:8080/portal
but for some reason when I access
http://localhost:8080/
the browser redirects to
https://localhost/portal
instead of redirecting to:
http://localhost:8080/portal
Locations:
upstream webportal {
server portal_container:8080;
}
location / {
return 301 /portal;
}
location /portal {
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_pass http://webportal;
}
I was able to fix the problem by adding absolute_redirect off; to my location:
location / {
absolute_redirect off;
return 301 /portal;
}

nginx reverse proxy "catch-all" location

EDIT: To be more clear, this is nginx version 1.13.8.
Take the following as an example nginx.conf file:
http {
upstream portal_backend {
server pc-loc43-01:15080;
}
upstream auth_backend {
server pc-loc43-01:16080;
}
server {
listen 9080 default_server;
server_name my-reverse-proxy;
location / {
auth_basic off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_pass http://portal_backend/;
}
location /auth {
auth_basic off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_pass http://auth_backend/auth;
}
}
}
I want to configure nginx to default to location / if it is unable to match the request to any of the locations, but I cannot find how to do this.
I don't see anything wrong with your code.
location / { is already the default location block for "unhandled" locations.
This would match all locations:
location / {
# ...
}
This would match the root only:
location = / {
# ...
}
This will match /auth and sub directories:
location /auth {
# ...
}
It must be related to how nginx does request matching -- somehow auth and authorize are too similar and it causes nginx problems (not a great explanation and maybe someone more experienced with nginx internals can chime in). The "solution" was to duplicate location / into location /authorize, so now the config file looks like:
...
location / {
auth_basic off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_pass http://portal_backend/;
}
location /authorize {
auth_basic off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_pass http://portal_backend/;
}
...
All the other routes work as I would have expected, e.g. /users, /customers, /whatever are all handled by location /

Static content is not loading with sub url with Nginx Server Try files

I have new to Nginx server. I have site with url and I am using try_file with Ngnix server configuration. These are some url:
http://localhost:81/test-url
http://localhost:81/test-url/abc
If am try to fetch this url
http://localhost:81/test-url
then it work fine and all css will be loaded fine.But when I am try to fetch this url
http://localhost:81/test-url/abc
then It css and js file is not loaded. Because css path will be changed to
http://localhost:81/test-url/css/style.css
but I actual path need is
http://localhost:81/css/style.css
My Nginx Server Config is
upstream dev {
server 127.0.0.1:9130;
}
server {
rewrite_log on;
listen [::]:81;
server_name localhost;
location / {
root D:/project/src/main/webapp;
try_files $uri /index.html;
}
location ~ ^/app/.+ {
proxy_pass http://dev;
proxy_set_header Host $http_host;
}
location ~ /api/.+ {
proxy_pass http://dev;
proxy_set_header Host $http_host;
}
location ~ /client/.+ {
proxy_pass http://dev;
proxy_set_header Host $http_host;
}
location ~ /admin/.+ {
proxy_pass http://dev;
proxy_set_header Host $http_host;
}
location ~ /oauth/.+ {
proxy_pass http://dev;
proxy_set_header Host $http_host;
}
location ~ /webhook/.+ {
proxy_pass http://dev;
proxy_set_header Host $http_host;
}
}
So, Please any one can help me. How to resolve this issue with Nginx server.I am using angular url to load website.
I have found the solution by using adding
<base href="/">
at the top in head tag.

Nginx lost my static images for one site in sites-enabled

I have two sites in sites-enabled for nginx:
1) project - this is essentially the top level domain - mysite.com
server {
listen 80;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/project/static/;
}
}
2) blog - this is for a blog, that is accessible via: mysite.com:8080
server {
listen 8080;
location blog/ {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/blog/static/;
}
}
Nginx has accepted both configurations, but when I visit 1 i get all of the text, but no css, or images.
If I visit 2 i get everything working fine.
What am I doing wrong?
Thank you
For the alias directive to work correctly, the URI in the location and the URI in the alias directive should both end in / or neither end in /. The algorithm seems to substitute one string for the other.
So you should probably write:
location /static {
alias /home/www/flask-deploy/project/static;
}
However, when the last element(s) of the alias match the location, the root directive is preferred (see this):
location /static {
root /home/www/flask-deploy/project;
}
Your location blog/ looks incorrect, prefix locations should always begin with a /.

Resources