nginx reverse proxy loop - nginx

I need to:
point site.com to my site.github.io (that works)
point site.com/anything to filesystem /site/dist (that works)
point site2.com to site.com to filesystem /root/dist (that doesn't work, here it shows site.github.io)
site.com.conf
server {
listen 80;
server_name site.com;
location = / {
proxy_pass http://site.github.io;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
root /site/dist;
try_files $uri /index.html;
}
}
site2.com.conf
server {
listen 80;
server_name site2.com;
proxy_set_header Host site.com;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://127.0.0.1/$request_uri;
}
}

In the case http://site2.com you're going to end up with uri = "/" when hitting site.com, which you have set to proxy to site.github.io.
You may want to do instead for site2 is:
location / {
proxy_pass http://127.0.0.1/site2/$request_uri;
}
Then in site.com:
location ~ ^/site2(.*)$ {
root /site/dist;
try_files $1 /index.html;
}
OR
location /site2 {
rewrite ^/site2(.*)$ $1 break;
root /site/dist;
try_files $uri /index.html;
}

Related

Nginx SPA Serving Static Files, Proxying API Calls, and Rewriting to /index.html

So, I'd like to have an nginx.conf that...
Serves all /static requests directly
Proxies all /api requests to another local server (port 8200)
Serves /index.html for all other requests (so, /contact/123 would really serve /index.html)
Here is my current config...
server {
listen 80;
server_name www.xyz.io;
root /opt/xyz/www;
location / {
try_files $uri $uri/ #backend;
}
location #backend {
proxy_pass http://127.0.0.1:8200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
I'm really not clear how to have all other requests serve /index.html though. Ideas?
Try this:
server {
listen 80;
server_name www.xyz.io;
root /opt/xyz/www;
# index
index index.html;
# $uri, index.html
location / {
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://127.0.0.1:8200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}

nginx: location = / {} error

My nginx vhost config content:
server {
listen 80;
server_name t.xianzhi.xxx.domain;
access_log /data/log/nginx/t.xianzhi.xxx.domain_access.log main;
location ~ /\. {deny all;}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
location = / {
root /data/web/static/html;
index index.html;
}
location / {
proxy_pass http://127.0.0.1:9000;
}
location = /favicon.ico {
access_log off;
root /data/web/static/;
}
location = /apple-app-site-association {
add_header Content-Type "text/html; charset=utf-8";
root /data/web/show/public/wap/;
}
location ~ \.(css|js|png|jpg|woff|ttf)$ {
root /data/web/static;
expires 10d;
}
}
As the config, I want to server the path / to /data/web/static/html/index.html and server the others to proxy_pass.
The truth is the path / is 404 not found and the others is successful.
The log is :
24/Aug/2017:10:49:43 +0800 10.5.17.37 t.xianzhi.xxx.domain - curl/7.51.0 - request:GET / HTTP/1.1 bbs:233status:404 upad:127.0.0.1:9000 rspt:0.017 rqtt:0.017 request_body:-
So, the / is passed to proxy.
Some info:
The nginx version: nginx/1.10.1
So, how to fix it?
The problem is your = / location block. If you consider the section
location = / {
root /data/web/static/html;
index index.html;
}
You specify the root and index.html, but you don't server anything. So you should change it to
location = / {
root /data/web/static/html;
index index.html;
try_files /index.html =404;
}
or
location = / {
root /data/web/static/html;
try_files /index.html =404;
}

NGINX Rewrite to strip a URL Segment

I have moved my site to a new server running nginx from apache.
The old site structure was:
example.com/NEW/
The new structure:
example.com/
I have a few applications that still reference uploads/files like:
example.com/NEW/assets/images/7871862618261826.jpg
I need to rewrite all the requests that include the /NEW/ parameter.
eg:
example.com/NEW/assets/images/7871862618261826.jpg
should be routed to:
example.com/assets/images/7871862618261826.jpg
My Current Nginx config looks like:
server {
listen 80;
server_name www.domain.com domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443;
if ($http_host = domain.com) {
rewrite (.*) https://www.domain.com$1;
}
location /NEW/ {
rewrite ^/NEW(.*)$ $1 last;
#return 405;
#return 301 https://www.domain.com$request_uri;
}
server_name domain.com www.domain.com;
access_log /var/log/nginx/domaincom.access.log;
error_log /var/log/nginx/domaincom.error.log;
root /var/www/domain.com/public_html/;
index index.html index.htm index.php;
#set default location
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
How can I achieve the rewrite?
I Finally got it working with the below location block:
location ~ /NEW/.*\.(png|jpg|gif|pdf|doc)$ {
rewrite ^/NEW(.*)$ $1 last;
}
It seems the location block was not being executed as required.

nginx proxy_pass static assets

I have the following nginx configuration
rewrite_log on;
server {
server_name greymarmita.no-ip.org;
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
server {
server_name greymarmita.no-ip.org;
listen 443 ssl;
error_log /var/log/nginx/main.error;
access_log /var/log/nginx/main.access;
ssl on;
ssl_certificate /etc/ssl/localcerts/autosigned.crt;
ssl_certificate_key /etc/ssl/localcerts/autosigned.key;
root /srv/www;
index index.html /index.html;
location /rasp/ {
proxy_pass http://192.168.2.6:81/;
}
location /cam/ {
proxy_pass http://192.168.2.4:8081;
}
location ^~ /router/ {
proxy_pass http://192.168.2.1/;
}
location /nas/ {
proxy_pass http://192.168.2.13/;
proxy_redirect 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;
}
}
However when I try to access http://192.168.2.6/nas although the html files are served correctly, files under /web/ are not
GET https://greymarmita.no-ip.org/web/images/login.png 404 (Not Found)
The correct path for these assets should be https://greymarmita.no-ip.org/nas/web/images/login.png
You don't have a location to match that file, try adding this
location / {
try_files $uri $uri/;
}

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