Nginx redirecting too many times - nginx

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

Related

nginx proxy_pass shows different app and crosses out https

When I try to visit app2, this happens:
It works with http://123.456.789.255:8081
It doesn't work with https://app2.example.com, it goes to https://app2.example.com with https crossed out, but shows app1.
Why does this happen?
Here's the nginx configuration.
There are also A records for both subdomains.
events {}
http {
include mime.types;
proxy_connect_timeout 999;
proxy_send_timeout 999;
proxy_read_timeout 999;
send_timeout 999;
# APP 1 =============================================================
server {
listen 80;
server_name app1.example.com;
return 301 https://app1.example.com$request_uri;
}
server {
listen 443 ssl;
server_name app1.example.com;
ssl_certificate /etc/letsencrypt/live/app1.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app1.example.com/privkey.pem;
location / {
proxy_pass 'http://localhost:3000/';
}
}
# APP 2 ============================================================
server {
listen 80;
server_name app2.example.com;
return 301 https://app2.example.com$request_uri;
}
server {
listen 443 ssl;
server_name app2.example.com;
ssl_certificate /etc/letsencrypt/live/app2.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app2.example.com/privkey.pem;
location / {
proxy_pass 'http://127.0.0.1:8081';
}
}
}

How to mask sub-domain using nginx

I have nodejs application working with a main domain, and its connecting thought nginx proxy pass to nodejs application, with redirection rule , as per the redirection rule if request is coming from mobile, it will redirect into a sub-domain, it is working fine.
both main domain and sub-domain are different version ,and running deferent server.
Now I want to mask this sub-domain , I don't want to see my sub-domain in browser, how can I do this.
Below mentioned my nginx conf.
server {
listen 80;
server_name mydomain;
return 301 https://$server_name$request_uri;
}
include mime.types;
server {
listen 443 ssl;
server_name mydomain;
ssl_certificate /etc/ssl/CA_Bundle.ca-bundle;
ssl_certificate_key /etc/ssl/2021/server.key;
location / {
add_header Access-Control-Allow-Origin *;
proxy_set_header Host $host;
proxy_pass http://172.17.0.1:8080;
proxy_redirect off;
expires off;
set $agentflag 0;
if ( $http_user_agent ~* "(Mobile)" ){
set $agentflag 1;
}
if ($request_uri ~ "user-agent"){
set $agentflag 0;
}
if ( $agentflag = 1 ) {
rewrite ^/(.*)/$ /$1 permanent;
return 307 https://mobile.mydomain$request_uri;
}
}
}
server {
listen 80;
server_name mobile.mydomain;
return 301 https://$server_name$request_uri;
}
include mime.types;
server {
listen 443 ssl;
server_name mobile.mydomain;
ssl_certificate /etc/ssl/CA_Bundle.ca-bundle;
ssl_certificate_key /etc/ssl/2021/server.key;
location / {
add_header Access-Control-Allow-Origin *;
proxy_set_header Host $host;
proxy_pass http://172.17.22.1:8081;
proxy_redirect off;
expires off;
}
}
It is working fine now,
I have used proxy_pass , instead of rewrite.
if ( $agentflag = 1 ) {
proxy_pass http://IP-address-mobile-app;

Redirection www and non www to one server

I would like to redirect my website after entering www.lombo.pl to only lombo.pl (with SSL certificate).
Now when something write www.lombo.pl it does not redirect. I tried to change my nginx file but still to no avail. The user can visit my website via www.lombo.pl (which at the same time shows an error because I do not have a SSL certificate configured for this domain).
upstream app_server {
server unix:/home/app/run/gunicorn.sock fail_timeout=0;
}
server {
#listen 80;
# add here the ip address of your server
# or a domain pointing to that ip (like example.com or www.example.com)
listen 443 ssl;
server_name lombo.pl;
ssl_certificate /etc/letsencrypt/live/lombo.pl/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/lombo.pl/privkey.pem;
server_name 157.245.228.127;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/app/logs/nginx-access.log;
error_log /home/app/logs/nginx-error.log;
location /static/ {
alias /home/app/static/;
}
# checks for static file, if not found proxy to app
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
server {
listen 80;
server_name lombo.pl;
return 301 https://$host$request_uri;
}
server {
listen 80;
server_name www.lombo.pl;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name www.lombo.pl;
return 301 https://$host$request_uri;
}

Nginx HTTP to HTTPS 301 loop redirect

I need to make HTTP->HTTPS redirection for whole site, but every time I get error message with 301 loop redirection. Please correct my conf to not get 301 error.Here is my conf file:
upstream live {
server IP:PORT;
}
server {
listen 80 default;
server_name mysite.com;
access_log off;
error_log off;
root /usr/share/nginx/html/;
index index.html index.htm;
return 301 https://$host$request_uri;
}
server {
listen 443;
root /usr/share/nginx/html/;
index index.html index.htm;
rewrite_log on;
server_name mysite.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug;
client_max_body_size 200m;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html?$uri&$args;
}
location /web {
proxy_pass https://live/gateway/web;
proxy_set_header "MP_FRONT" aaa;
proxy_pass_request_headers on;
}
}
Just try to replace first server JSON like below
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
please, try and let me know its working for you or not

How to turn off ssl for specific url?

Here is my config:
upstream example {
server unix:///home/deploy/apps/example/shared/tmp/sockets/example-puma.sock;
}
server {
listen 80 default_server;
server_name example.org;
location /non_https {
proxy_pass http://example;
}
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
include snippets/ssl-params.conf;
root /home/deploy/apps/example/current/public;
try_files $uri/index.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
keepalive_timeout 10;
}
I need to turn off ssl for /non_https/* how to get it?
If you place the retuen 301 inside a location "/" block it should work:
server {
listen 80 default_server;
server_name example.org;
location /non_https {
proxy_pass http://example;
}
location / {
return 301 https://$server_name$request_uri;
}
}

Resources