Possible to match location directive in nginx with a port number? - nginx

I have a config that looks like:
1)
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) updated
server {
listen 8080;
server_name mysite.com;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
root /home/www/flask-deploy/blog;
}
}
Updated issue:
If I view mysite.com:8080/static/css/main.css , i can see this, but if i visit mysite.com:8080, it will show the content for #1 not #2

The first configuration includes alias directive and the second consists root. So, that is the reason for the difference in their behaviour. Here are examples of matching URIs and real path in fs for both configurations:
1
location /static {
alias /home/www/flask-deploy/project/static/;
}
http://somehost:80/static/somefile -> /home/www/flask-deploy/project/static/somefile
http://somehost:80/static/dir/somefile -> /home/www/flask-deploy/project/static/dir/somefile
2
location /static/ {
root /home/www/flask-deploy/blog/static;
}
http://somehost:8080/static/somefile -> /home/www/flask-deploy/blog/static/static/somefile
http://somehost:8080/static/dir/somefile -> /home/www/flask-deploy/blog/static/static/dir/somefile

Related

Regex in nginx inside map

I cannot fix routing in nginx for different parts in URI . So if the request has URI starting with de it should pass the traffic to app_b. That does not happen and I'm getting error: invalid URL prefix in "http://"
Here's the config.
map $request_uri $resources_location {
"/" "app_a:1234/";
"^.*de.*$" "app_b:2345/";
}
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://$resources_location;
proxy_redirect off;
}
location ~ ^/(assets|public|favicon.ico) {
proxy_pass http://$resources_location;
}
}
How that can be solved (also with a help of map)?

Using proxy_set_header with proxy_redirect?

We have a nuxt app, and we need to have:
https://outer.domain.com/windows/thank-you
to be proxied to:
https://inner.domain.com/thank-you
I tried this:
server {
server_name outer.domain.com;
listen 80;
location /windows {
location /windows/ {
proxy_set_header Host inner.domain.com;
proxy_pass http://127.0.0.1:4300/;
}
location /windows/thank-you/ {
proxy_set_header Host inner.domain.com;
proxy_redirect http://$host/windows/thank-you/ /;
proxy_pass http://127.0.0.1:4300;
}
}
}#server block
But proxy_redirect seems not working, is there anything I am missing?

nginx: not matching the correct way

i have the following nginx configuration
GIVES WRONG RESULTS
upstream webapp {
server webapp:8000;
}
upstream db {
server phppgadmin:80;
}
server {
listen 80;
server_name db.*;
location / {
proxy_pass http://db;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
server {
listen 80;
location / {
proxy_pass http://webapp;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /static {
autoindex on;
alias /staticfiles/;
}
location /media {
autoindex on;
alias /mediafiles/;
}
}
My ip address of the pc is xx.xx.xx.xx
what I observed is that
db.xx.xx.xx.xx - shows the db upstream
and also xx.xx.xx.xx - shows the db upstream
GIVES CORRECT RESULTS
where as when i change the order it shows properly
upstream webapp {
server webapp:8000;
}
upstream db {
server phppgadmin:80;
}
server {
listen 80;
location / {
proxy_pass http://webapp;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /static {
autoindex on;
alias /staticfiles/;
}
location /media {
autoindex on;
alias /mediafiles/;
}
}
server {
listen 80;
server_name db.*;
location / {
proxy_pass http://db;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
Now
db.xx.xx.xx.xx - shows the db upstream
and xx.xx.xx.xx - shows the webapp upstream
QUESTION
I am not able to understand in the first case how come xx.xx.xx.xx is matched by server_name db.*; Or why the second one shows the intended behaviour
note
Ofcourse in my /etc/hosts i have setup
xx.xx.xx.xx app.xx.xx.xx.xx
xx.xx.xx.xx db.xx.xx.xx.xx
Nginx selects server block by port (with IP, if given) and Host header. If there is no match, it uses a block where default_server is set. In your case there is no match by Host and neither there is a default_server so I think it just picked first. Either add server_name to the block with the webapp upstream or make it a default one:
listen 80 default_server;

How do I make this directory public with nginx?<

I have a ghost.org pre-built droplet on DigitalOcean that I'm willing to use for a music production blog. This is what my site conf looks like:
server {
listen 80;
listen [::]:80;
server_name mydomain.com;
root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2369;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
}
I want to serve some html pages I uploaded at this directory /var/www/downloads when people visits my domain.com/downloads. How do I do it leaving everything else working? I've tried this
location /downloads/ {
alias /var/www/downloads/;
}
but it did not work. Any help?
Thanks!
Try this:
location /downloads {
root /var/www;
}

configuring subdirectory app in nginx

Can someone help me configure my nginx where:
/app -> is the local html pages
/ -> is the https server which does not have app folder
The following conf didnt work for me: (
server {
listen 80;
ssl_verify_client off;
server_name localhost;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass https://xx.xx.xx.xx/;
}
location /app {
root "c:\LOCAL_PATH\app";
index index.html;
}
}
Exhanging your location /-block for the following oughta do it:
location / { rewrite ^ https://$host$request_uri?; }

Resources