ember, nginx, reverse proxy - nginx

I have my app deployed to a file server here at:
http://example.com/my-team/my-app
We want our app to be facing at:
http://amazingapp.com
So far this is what I have on nginx
server {
listen 80;
server_name http://amazingapp.com;
location / {
proxy_pass http://example.com/my-team/my-app/;
}
}
This works for initial http://amazingapp.com but any subsequent requests http://amazingapp.com/post/1/comment/2 result in 404
Most of the ember deploy examples have the file being served from the same location as the web server, using try_files $uri $uri/ index.html So i don't believe i'm able to go down that path?
I've tried using regex on the location but that requires the proxy_pass to have parameters.
If you could point me in the right direction would be so grateful.
Thanks in advance!

Consider your config below
server {
listen 80;
server_name http://amazingapp.com;
location / {
proxy_pass http://example.com/my-team/my-app;
}
}
When you access http://amazingapp.com/abc your location / will cut abc out of it and append to proxy_pass. Making your url as http://example.com/my-team/my-appabc. Fix is quite simple, just add a trailing/` to your proxy_pass
server {
listen 80;
server_name http://amazingapp.com;
location / {
proxy_pass http://example.com/my-team/my-app/;
}
}

Related

serving a file to loader.io in NGINX is not working

server {
listen 80;
listen [::]:80;
server_name _;
root /home/ec2-user;
# location / {
# proxy_pass http://sdc_servers;
# }
location /loaderio-73cc9f7580b0a0844a502ff1c98e9305.txt {
proxy_pass http://3.15.28.77/loaderio-73cc9f7580b0a0844a502ff1c98e9305/;
}
}
I am not sure where I am going wrong trying to serve this file, I am totally lost
location /loaderio-73cc9f7580b0a0844a502ff1c98e9305.txt { proxy_pass http://3.15.28.77/loaderio-73cc9f7580b0a0844a502ff1c98e9305/; }
Instead of passing the token in the proxy_pass, try something like
location /loaderio-9f8a9889007f8a2721b6245b80344b75.txt { root /<location_to_the_file>/loader/; try_files /loader.txt =404; }
loader - directory containing loader.txt which contains the actual
token provided by loader.io.

How do i use NGINX to reverse proxy to local react app

I'm trying to do a simple reverse proxy to my local react app with nginx. I can't wrap my head around how this works. Do i need a root variable in location /test or maybe a alias? because nginx is looking in the wrong address. (im running my react app locally at localhost:3001)
Already tried using rewrite /test(.*) /$1 break in the "location /test"-block
this is my nginx.conf:
server {
listen 81 ;
server_name app1.localhost;
location / {
root html;
index index.html index.htm;
}
location /test {
proxy_pass http://localhost:3001;
}
}
heres the console log when i try to enter app1.localhost:81/test:
Just go with two server blocks:
server {
listen 81 default_server;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name app1.localhost;
location / {
proxy_pass http://localhost:3001;
}
}

multiple location blocks in nginx and proxy pass

I am trying to run 2 applications behind an NGINX server.
one is listening on 3000 (grafana) and one is listening on 9090 (prometheus)
My current server block nginx.conf looks like this:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
location /prometheus{
proxy_pass http://localhost:9090/;
}
}
Now for Grafana, this works perfectly and everything works in the dashboard.
But when typing Domain/prometheus it still redirects me to grafana instead of Prometheus.
Do I need todo something specific to have it working in this setup that everything besides /prometheus is redirected to grafana?
By default, http://localhost:9090 will be redirected to http://localhost:9090/graph, so the request is being redirected as below:
# the origin request
http://Domain/prometheus
# the request redirect by nginx
http://localhost:9090/
# the request redirect by prometheus
http://Domain/graph
# the request redirect by nginx again
http://localhost:3000/graph
You can check this by using F12 in Chrome.
To fix this, I recommend you separate the domain into two domains:
server {
listen 80;
server_name Domain-Grafana; # Domain for Grafana
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
}
server {
listen 80;
server_name Domain-Prometheus; # Domain for Prometheus
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:9090/;
}
}
So I solved it now by doing this:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location /prometheus {
proxy_pass http://localhost:9090/prometheus;
}
location / {
proxy_pass http://localhost:3000/;
}
}

nginx proxy_pass removing base path

So I currently have a site at http://www.example.com and I would like that all requests from
http://www.example.com/api/v2/<anything>
to be proxied to
http://api.v2.com/<anything>
Note that http://api.v2.com does not have a base path.
My config:
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
This way, however, the requests are being proxies to http://api.v2.com/api/v2/<anything> keeping the original path, and not http://api.v2.com/<anything>.
I've already noticed that:
location /api/v2 {
proxy_pass http://api.v2.com/api;
works as desired - http://api.v2.com/api/v2/<anything> proxies to http://api.v2.com/api/<anything>.
Is there a way to achieve the first behavior (that is , http://api.v2.com/api/v2/<anything> to http://api.v2.com/<anything>)? Do I have to use a rewrite? I know it it related to normalized URIs and that is expected, just would like to know if there's a way to do it.
If you want to remove the proxy path from resulting url to the server, either you should have the uri part in the proxy url, or you should specify the rewrite part.
In this specific case, it should be like as follows,
location /api/v2/ {
rewrite /api/v2/(.*) /$1 break;
proxy_pass http://api.v2.com/;
}
for more information visit nginx documentation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com/;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Nginx proxy_next_upstream doesn't work

I want nginx to search my local host for the file first and on a 404 error it should search server 1.1.1.1.
I am able to fetch the file that is located on local host, but not able to get from server 1.1.1.1.
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log main;
location /products/ {
proxy_next_upstream http_404;
root /var/foo;
}
}
server {
listen 80;
server_name 1.1.1.1;
location /products/ {
#########
}
}
I guess proxy_next_upstream is not switching to the server.
Any help on this would be appreciated.
The proxy_next_upstream directive is a configuration directive to control re-request from a group of upstream servers by a proxy_pass if request to one of them fails. It doesn't make sense without proxy_pass and an upstream block defined. You may use it if you proxy to multiple upstream servers like this:
upstream backends {
server 192.2.0.1;
server 192.2.0.2;
...
}
server {
...
location / {
proxy_pass http://backends;
proxy_next_upstream error timeout http_404;
}
}
If you want nginx to search for a file on disk, and if it's not found - proxy request to another server, configure it e.g. using try_files fallback instead:
location / {
root /path/to/root;
try_files $uri #fallback;
}
location #fallback {
proxy_pass http://...
}
See http://nginx.org/r/try_files for more info about the try_files directive.

Resources