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.
Related
I have created a custom 404 page for resources that dont exist.
It works with all endpoints except the ones which have /api/v1/, where I get the default NGINX 404 page.
I have domain.name.conf file in /etc/nginx/conf.d/:
server {
listen 80;
listen [::]:80;
server_name domain.name www.domain.name;
root /var/www/domain.name/public_html;
error_page 404 /not_found.html;
location /api/v1/ {
proxy_pass http://localhost:8080/;
limit_except GET HEAD { deny all; }
}
location / {
index index.html;
try_files $uri $uri/ =404;
}
location = /not_found.html {
internal;
}
}
On adding the $try_files directive inside /api/v1/, "Hello, World!" from the backend REST API is not displayed at /api/v1/hello, even though its a valid endpoint. Instead I get the custom 404 error page:
server {
listen 80;
listen [::]:80;
server_name domain.name www.domain.name;
root /var/www/domain.name/public_html;
error_page 404 /not_found.html;
location /api/v1/ {
proxy_pass http://localhost:8080/;
try_files $uri =404;
limit_except GET HEAD { deny all; }
}
location / {
index index.html;
try_files $uri $uri/ =404;
}
location = /not_found.html {
internal;
}
}
How can I use a single custom error page for all non existing resources ?
Thanks to Richard Smith's comment, the conf file look like:
server {
listen 80;
listen [::]:80;
server_name domain.name www.domain.name;
root /var/www/domain.name/public_html;
error_page 404 /not_found.html;
proxy_intercept_errors on;
location /api/v1/ {
proxy_pass http://localhost:8080/;
limit_except GET HEAD { deny all; }
}
location / {
index index.html;
try_files $uri $uri/ =404;
}
location = /not_found.html {
internal;
}
}
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.
I am struggling to implement an automatic nginx redirect from non index pages to my index page, with the exception of /admin
For instance, example.com/test should redirect to example.com, but example.com/admin should not redirect to example.com
This is my current nginx configuration file:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
I have tried adding a try_files statetement within my location / block, and other things, but none seem to work. Am I missing something?
You are trying to mix proxy_pass with try_files, it won't work within the same location block. You can use named location instead and rewrite any URI that doesn't start with /admin to a root one using negative regex assertion:
location / {
try_files $uri #app;
}
location #app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
You don't need the separate location /media { ... } or location /static { ... } blocks, because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
Instead you just need to define the common server root (outside of any location blocks):
root /var/www/example.com;
You are also don't need to use the proxy_ssl_server_name directive since you are not proxying your request to the upstream with HTTPS protocol.
I have the following Nginx configuration file...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location /common/ {
root /etc/nginx/html/common;
}
}
And the folder structure is like so...
html\app1
html\common
When I try to browse...
http://localhost/ > Works
http://localhsot/index.html > Works
http://localhost/common/somefile.txt > Doesn't work
What am I missing?
You should use alias instead of root:
server {
listen 80;
server_name 127.0.0.1 localhost;
location / {
root /etc/nginx/html/app1;
index index.html;
}
location /common {
alias /etc/nginx/html/common;
}
}
If you use root in common the 127.0.0.1/common/somefile.txt will try /etc/nginx/html/common/common/somefile.txt (notice the two common). If you check nginx's logs you can see it.
I am adding my own answer since I finally got it working. Posting it here, so it might help others...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
root /etc/nginx/html;
}
}
Basically, the way Nginx was trying was /etc/nginx/html/common/common. Removing the common from root worked. Also found that http://localhost:8888/common/ needed to have a trailing /.
Because it firstly match the location /. You can do it like this:
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
root /etc/nginx/html/common;
}
}
EDIT:
Yeah. It seems some complicated. You can do it like this:
First, you need create a new server:
server {
listen 80;
server_name common.com; # A virtual host
root /etc/nginx/html/common;
}
Then, you need modify the config above like this:
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
rewrite ^/common(/.*)$ $1 break; # rewrite the /common/
proxy_set_header Host common.com; # it will requests common.com which the server of 127.0.0.1. then will match the above server.
proxy_pass http://127.0.0.1;
}
}
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;
}