How to check if a word includes in whole url in nginx - nginx

I have same domain. I want to redirect anything starting with /v2/xyzz/abcc to new admin proxy but it still redirects to / here is my nginx.
domain.com/v2/as/as/asss -> new_admin
domain.com/ -> to old_admin
upstream new_admin {
server 127.0.0.1:3000 fail_timeout=0;
}
upstream old_admin {
server 127.0.0.1:4063 fail_timeout=0;
}
server {
listen 80;
server_name domain.com;
location ~ / {
include /etc/nginx/config/proxy.conf;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_pass http://old_admin;
}
location ~ ^/(v2)(/.*) {
include /etc/nginx/config/proxy.conf;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_pass http://new_admin;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

Related

nginx is not returning 200 but going through the api server and returning 400

I'm trying to return a 2xx status code from a specific path using nginx.
this path should support POST only.
This is the code I have:
location = /v1/rest/v1/example/webhook {
return 200;
break;
}
But it kept bypassing the code above, and went straight to the api server (/v1) which I'm trying to prevent.
Here is an overview of related blocks:
server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
server_name staging.example.com, api.staging.example.com;
root /usr/share/nginx/html;
location = /v1/rest/v1/example/webhook {
return 200;
break;
}
location / {
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_request_buffering off;
proxy_pass http://HOST:PORT;
server_name_in_redirect off;
}
location ^~ /v1 {
rewrite /v1/(.*) /$1 break;
limit_req zone=nginx_main burst=50 nodelay;
limit_req_status 444;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_request_buffering off;
proxy_pass http://HOST:PORT;
server_name_in_redirect off;
auth_basic off;
}
error_page 400 404 /40x.html;
location = /40x.html {
limit_req zone=nginx_main burst=10 nodelay;
limit_req_status 444;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
limit_req zone=nginx_main burst=10 nodelay;
limit_req_status 444;
}
}
Placing the block on top of the others did not help.

Nginx redirecting too many times

I am running into an issue where nginx is somehow redirecting over and over but I don't understand why:
server {
listen 80;
server_name splunk.trever.me;
return 301 https://splunk.trever.me$request_uri;
}
# Port 443 https config
server {
listen 443 ssl http2;
server_name splunk.trever.me;
location / {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_pass http://10.0.1.1:8000;
}
access_log /var/log/nginx/splunk.trever.me/access.log;
error_log /var/log/nginx/splunk.trever.me/error.log;
}
Am I missing something here? I looked at all the documentation and it doesnt seem to be wrong. Does this mean something upstream is sending the browser back to http?
use this:
server {
server_name splunk.trever.me;
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
location / {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_pass http://10.0.1.1:8000;
}
access_log /var/log/nginx/splunk.trever.me/access.log;
error_log /var/log/nginx/splunk.trever.me/error.log;
}
You can redirect the requests using the IF directive in nginx config.
server {
listen 80;
listen 443;
server_name splunk.trever.me;
if ($scheme = http) {
rewrite ^/(.*)$ https://splunk.trever.me/$1 permanent;
}
location / {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_pass http://10.0.1.1:8000;
}
access_log /var/log/nginx/splunk.trever.me/access.log;
error_log /var/log/nginx/splunk.trever.me/error.log;
}

NGINX : Block from location and if caught parameter is empty

I'm stuck in an existing issue. I'm trying to deny user if they tried to access a certain location page without a parameter that I've configured in nginx.conf
by using this parameter $remote_user
So is there any way i can include in the location , if i detect the remote user is empty and accessing the link. Can i deny them?
location ^~ /eas/monitor_alarm/
{
proxy_pass http://myserver.com/monitor_alarm/;
}
Default.conf
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
keepalive_timeout 90;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
send_timeout 90;
return 301 https://$server_name$request_uri;
location ~ (?:^|/)\. {
deny all;
}

How to redirect port 80 to different server in nginx?

In my use case, I would like to redirect www.xyz.com/static to www.abc.com using nginx as reverse proxy. I have the following nginx configuration in place,
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
keepalive_timeout 65;
client_max_body_size 500M;
#timeouts
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
upstream s-content {
server abc.com;
keepalive 64;
}
#
# The default server
#
server {
listen 80;
server_name rproxy;
location /static {
rewrite /(.*) /$1 break;
proxy_pass http://s-content;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
#proxy_set_header Connection "";
#proxy_http_version 1.1;
}
# redirect not found pages to the static page /404.html
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
This server proxy is mapped to xyz.com and listens on port 80. I would like to redirect to abc.com when I get a request in abc.com/static. But for some reasons the rewrite is not working.
Could someone help me on this. I appreciate the help.

nginx gateway timeout after 1minutes 20 seconds

My nginx settings are like this :
server {
listen 80;
server_name websitenamne.in www.websitename.in;
root /root/path;
location /static {
}
location / {
proxy_pass http://localhost:8003/;
proxy_connect_timeout 3600;
send_timeout 3600;
proxy_read_timeout 3600;
include /etc/nginx/proxy_params;
}
# what to serve if upstream is not available or crashes
error_page 500 502 503 504 /static/50x.html;
}
In the above , at location /, I added these things :
proxy_connect_timeout 3600;
send_timeout 3600;
proxy_read_timeout 3600;
But still nginx sending Gateway timeout error . How can I solve this ?
It's generally not possible to set connect timeout longer than a time your OS will re-try SYN packet transmissions, usually 75 seconds as indicated in nginx docs.

Resources