How to correctly set root for nginx server? - nginx

I must set root in server, not in location. If I set it this way:
server {
listen 80;
server_name www.mydomain.com;
location / {
root /opt/html;
index index_sc.html;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
}
Then I can access /imgproxy content without problem. But if I move the root out of location like below, /imgproxy will cause 404 error:
server {
listen 80;
server_name www.mydomain.com;
root /opt/html;
location / {
index index_sc.html;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
}
I want to know how exactly can I set root in server and still has location /imgproxy/ work? Thanks!

I cannot reproduce this behaviour, but you can explicitly handle the /imgproxy URI by adding another location block:
root /opt/html;
location / {
index index_sc.html;
}
location = /imgproxy {
return 301 /imgproxy/;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
Ensure that you clear the browser cache between changes to the nginx configuration.

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;
}
}

How to exact match a nginx location?

I am configuring a nginx revser proxy. The result should be when user type http://10.21.169.13/mini, then the request should be proxy_pass to 192.168.1.56:5000. Here is the nginx config:
server {
listen 80;
server_name 10.21.169.13;
location = /mini {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
}
The above location block never worked with http://10.21.169.13/mini. The only location block worked is:
server {
listen 80;
server_name 10.21.169.13;
location / {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
}
But the above config also match http://10.21.169.13 request which is too board.
What location block will only match 'http://10.21.169.13/mini` and no more?
UPDATE: tried and failed with the following:
location /mini {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
location /mini/ {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
The error is request not found.
Try this out:
server {
listen 80;
server_name 10.21.169.13;
# root /usr/share/nginx/html;
# index index.html index.htm;
location / {
# add something here to handle the root
# try_files $uri $uri/ /index.html;
}
location /mini {
proxy_pass http://192.168.1.65:5000;
include /etc/nginx/proxy_params;
}
}
Let me know if that works for you.

Nginx Reverse Proxy Multiple Applications on One Domain

like these:
test1.sec.com 192.168.1.8:8001<br>
test2.sec.com 192.168.1.8:8002<br>
test3.sec.com 192.168.1.8:8003<br>
http://www.sec.com/test1/ 192.168.1.8:8001<br>
http://www.sec.com/test2/ 192.168.1.8:8002<br>
http://www.sec.com/test3/ 192.168.1.8:8003<br>
how to config the nginx.conf ?
You haven't provided much information, but based on what you gave, this should work:
server {
listen 80;
server_name test1.sec.com;
location / {
proxy_pass http://192.168.1.8:8001;
}
}
server {
listen 80;
server_name test2.sec.com;
location / {
proxy_pass http://192.168.1.8:8002;
}
}
server {
listen 80;
server_name test3.sec.com;
location / {
proxy_pass http://192.168.1.8:8003;
}
}
Then, for your www.sec.com, you'll need to add separate location blocks to catch the /test/ URIs.
server {
listen 80;
server_name www.sec.com;
location /test1/ {
redirect 301 $scheme://test1.sec.com;
}
location /test2/ {
redirect 301 $scheme://test2.sec.com;
}
location /test3/ {
redirect 301 $scheme://test3.sec.com;
}
#Rest of your config here...
}
Note: You have to specify your test location blocks before your root (/) unless you use a modifier to give them precedence.

How to rewrite localhost:9292/json to localhost:80/ using Nginx reverse proxy?

server {
listen 80;
server_name localhost;
location / {
index index.html;
root /Users/Lin/Codes/JS/Emberjs/yeoman-ember/dist;
}
location ~* ^/json {
root
proxy_pass http://localhost:9292;
}
}
The configure kinda works, but it only pass
localhost:9292/json to localhost/json.
But What I want is
localhost:9292/json to 'localhost'
`localhost:9292/json/post to 'localhost/post'
I think what I need to do is set root or do some rewrite, Anyone has some idea?
add a rewrite rule before the proxy_pass
location /json {
rewrite ^/json(.*) $1;
proxy_pass http://localhost:9292;
}

Resources