Why does proxy_pass cause try_files to fail? - nginx

my config:
server {
listen 80;
listen [::]:80;
error_log /dev/stdout debug;
root /usr/share/nginx/html;
location / {
proxy_pass http://www.google.com;
proxy_set_header Host www.google.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /subapp {
try_files $uri $uri/ /index.html;
# return 200 hint;
}
}
I use curl to test:
curl http://localhost/subapp/test
If I use return 200, I got "hint".
But when I use try_files, I get the proxy_pass result?
If I remove the proxy_pass, I get the correct result.

Related

How to rewrite dynamic sub domain to path URI in nginx?

I have a requirement where {any-dynamic-subdomain}.domain.com should rewrite to domain.com/{any-dynamic-subdomain} but not for www.domain.com
Example:
api.domain.com -> domain.com/api
api-1.domain.com -> domain.com/api-1
so on..
Note: Here subdomains are dynamic in nature.
Nginx version: openresty/1.11.2.2
Current nginx configs:
server {
listen 80;
server_name domain.com;
return 301 https://www.domain.in$request_uri;
}
server {
listen 80;
server_name www.domain.com;
set $upstream_endpoint backend-api.tools.com;
location / {
proxy_set_header HOST $upstream_endpoint;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300;
proxy_pass http://$upstream_endpoint;
}
------------------------
}
Kindly advise Nginx configurations for the same. Thanks
You will have to use a named capture in the server_name directive:
server {
listen 80;
server_name www.domain.com;
root /var/www/htmldoc;
index index.htm index.html;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name ~^(?<name>[^\.]+)\.domain\.com$;
set $upstream_endpoint backend-api.tools.com;
location / {
proxy_set_header HOST $upstream_endpoint;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300;
proxy_pass http://$upstream_endpoint/$name;
}
------------------------
}

Nginx reverse proxy returns 404 on static files

Basically I have two local application hosted on 2 different local port.
I'm trying to access http://localhost/admin which is serve via reverse_proxy but I got http://localhost/admin/main.css net::ERR_ABORTED 404 (Not Found).
Ive also tried below (without slash) but I still got the same error.
location /admin{
proxy_pass http://127.0.0.1:8090
....
}
app1.conf
server{
listen 80;
listen [::]:80;
root /var/www/first_app/dist;
index index.html;
access_log /var/log/nginx/first_app.access.log;
error_log /var/log/nginx/first_app.error.log;
location / {
try_files $uri $uri/ /index.html =404;
}
location /admin/{
proxy_pass http://127.0.0.1:8090/
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
client_max_body_size 0;
}
location ~* .(ico|jpg|png|gif|jpeg|css|swf|js|woff)$ {
access_log off;
gzip_static on;
gzip_comp_level 5;
expires 1M;
#add_header Cache-Control private;
add_header Cache-Control public;
}
}
app2.conf
server{
listen 8090;
listen [::]:8090;
root /var/www/second_app/dist;
location /{
try_files $uri /index.html;
}
}
It would be great if someone can explain to me what I'd miss? Thanks much.

nginx: how to divide /etc/nginx/conf.d/default.conf ?

looking for a way to divide /etc/nginx/conf.d/default.conf, something like per site. Any idea?
current file looks like this:
upstream Master_MAT {
server 172.18.0.3:8080;
}
upstream Master_PAT {
server 172.18.0.4:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
root /etc/nginx/html;
index index.html index.php;
#charset koi8-r;
location / {
root /etc/nginx/html;
try_files $uri /$uri $uri/ =404;
}
location /Master_MAT {
proxy_set_header Host $proxy_host;
proxy_pass http://Master_MAT/Master_MAT;
# proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /Master_PAT {
proxy_set_header Host $proxy_host;
proxy_pass http://Master_PAT/Master_PAT;
# proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Is there a way to put the Master_MAT in different file? Tried to use 'include' yet failed.
THX
Most people recommend using the sites-enabled and sites-available approach:
http {
…
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Now you can leave 'disabled' sites in sites-available and move them into the sites-enabled folder when you want it to be in use.
This is a wildcard so you can just create new .conf files for each site and it will load them automatically.
Here's an example of what would go inside /etc/nginx/sites-available/example.com
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

How to set reverse proxy for special path in nginx?

How to set reverse proxy for special path in nginx?
In my Nignx's default.conf:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
# root /usr/share/nginx/html;
root /var/www/html/website;
index index.html index.htm;
try_files $uri $uri/ /index.html;
proxy_pass http://107.120.30.76:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
I only know how to config the / root directory, how can I config the special path?
such as I request the http://107.120.30.76/api/* I want to transfer to http://107.120.30.76:8000/api/*. How to config this?
You can add the bellow configuration into the server config:
location /api/ {
# root /usr/share/nginx/html;
root /var/www/html/website;
index index.html index.htm;
try_files $uri $uri/ /index.html;
proxy_pass http://107.120.30.76:8001/api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

nginx proxy and backbone pushstate

I'm trying to set up nginx to work with my backbonejs application and api server.
The API server is external and being routed through https://website.com/api/...
Essentially, I want any non-matched urls to be routed to /index.html for the backbone app to handle.
I've tried using try_files, but that just overrides my API. I've tried setting up another location where I check if the request is a GET and also if it doesn't match register or login or api, but that also doesn't work. Here's my server so far:
server {
listen 80; ssl off;
listen 443 ssl;
server_name app.io;
ssl_certificate /etc/nginx/conf/ssl.crt;
ssl_certificate_key /etc/nginx/conf/app.key;
root /home/ubuntu/app/public;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
index index.html;
location / {
if ($scheme = "http") {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
location ~ ^/(api)|(auth).*$ {
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_pass https://app.aws.af.cm;
}
location ~ ^(/(register)|(login)).*$ {
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;
# GETs only
limit_except POST {
proxy_pass https://app.aws.af.cm;
}
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
Currently, try_files overrides the API and just redirects to index.html. Any idea how I can get everything to play nicely with one another?
Here's what I want:
if / - /index.html
else if /api/*|/auth/* - external proxy
else if /login|/register - POST - external proxy
else /* - /#$1
Figured it out:
Add try_files #uri #rewrites; to Location / and also add the #rewrites function below.
server {
listen 80; ssl off;
listen 443 ssl;
server_name app.io;
ssl_certificate /opt/nginx/conf/ssl.crt;
ssl_certificate_key /opt/nginx/conf/app.key;
root /home/ubuntu/app/public;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
index index.html;
location / {
if ($scheme = "http") {
rewrite ^ https://$http_host$request_uri? permanent;
}
try_files $uri #rewrites;
}
location ~ ^/(api)|(auth)|(logout)|(register)|(login).*$ {
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_pass https://app.cm;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location #rewrites {
rewrite ^/.+ /#$uri redirect;
}
}

Resources