Srping Security + Zuul + Nginx Authentication error handling - spring-boot

I have Zuul and Backend Srping Boot applications and it works just fine without nginx.
So normally it works like that:
User is at http://localhost:8080/auth/login
User types wrong login and password and sends it
User is redirected to http://localhost:8080/auth/login?error and is able to see error message.
Zuul is running on port 8080 and /auth/ is auth application running on another port but I can reach it through Zuul application without knowing exact location of auth application.
But with Nginx user is redirected back to http://localhost:8080/auth/login where ?error is missing and user can't see the error message.
I tried to configure Nginx to use https and to forward requests to my Zuul app that forwads requests to Spring application itself (where Spring Security is).
server {
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html login.html login.htm;
server_name servername.com; # managed by Certbot
location /auth/ {
access_log off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://zuul_ip:8080/auth/;
}
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/servename.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/servername.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = servername.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name servername.com;
return 404; # managed by Certbot
}
So I need user is redirected back correctly with query param is not deleted from url. How can I achieve this?

Related

Laravel Forge default site

We use Laravel Forge on a Load Balancer to handle a lot of sites on there. We always had one of the sites as a default/catch-all when a domain is pointed at us with no site conf set. Recently, that site's SSL expired. Took us a little bit but we got it back. Ever since then though, it has stopped being the catch-all. So if a site isn't pointing right, the invalid domain gets redirected to the first site in the list.
Here's a nginx conf for a site that redirects to the first server in the list.
FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.accuproadvisors.com/before/*;
# FORGE CONFIG (DO NOT REMOVE!)
include upstreams/www.accuproadvisors.com;
server {
listen 80;
listen [::]:80;
server_name www.accuproadvisors.com accuproaccounting.com;
server_tokens off;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate
# ssl_certificate_key
ssl_protocols TLSv1.2;
charset utf-8;
access_log off;
error_log /var/log/nginx/www.accuproadvisors.com-error.log error;
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.accuproadvisors.com/server/*;
location / {
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;
proxy_pass http://1127640_app/;
proxy_redirect off;
# Handle Web Socket Connections
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.accuproadvisors.com/after/*;
We have a 000-catch-all file and it contains:
server {
listen 80;
server_name _;
root /home/forge/catch-all;
index index.html index.htm;
error_page 404 /404.html;
location / { }
# return 404;
}
The folder /home/forge/catch-all contains the default index.html that was always the default until the SSL expired. Anyone have any tips? Anything is appreciated. Thanks!

Allow NGINX to send requests over http to another port

I have a React application running with NGINX which handles traffic on one port (www.domain.com - https) and I also have a back-end Spring Boot application which runs on a different port (www.domain.com:7080 - http).
Now NGINX serves 80, 443 ports and loads up my React application. My react application is hard-coded to send requests to www.domain.com:7080, however all requests fail. In the browser's console I can see the following error:
The page at 'https:// domain.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http:// domain.com:7080/auth/login'. This request has been blocked; the content must be served over HTTPS.
My NGINX configuration:
server {
listen 443 ssl; # managed by Certbot
root /var/www/ui;
server_name www.domain.com domain.com;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
index index.html;
}
}
server {
listen 80;
if ($host = domain.com) {
return 301 $host$request_uri;
} # managed by Certbot
server_name www.domain.com domain.com;
return 301 https://$host$request_uri; # managed by Certbot
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
My back-end application is served over http and I'd like to permit the front-end to talk to the back-end service.
I couldn't locate a similar question or tutorial on how I would go about solving this therefore I'm hoping to get some answers here :3
create api endpoint in your domain i.e. www.domain.com/api and configure nginx to pass traffic from that endpoint to your backend with proxy_pass directive. You'll have secure connection from your users and won't need to change anything in your backend server.

Nginx reverse proxy on SpringBoot redirect to /forums in nginx directory

I have a springboot application running on an Nginx server reverse proxy, inside of the nginx WWW root i have a forums directory i want to access via url/forums. So i am trying to setup a proxy for nginx so when someone goes to the website url/forums it will redirect to the nginx forums directory where i will have my PHP forums.
server {
listen 443 ssl; # Monitor port
server_name realmlands.com www.realmlands.com; # Domain name configuration, can be multiple
ssl_certificate /etc/letsencrypt/live/realmlands.com-0002/fullchain.pem; # Certificate address
ssl_certificate_key /etc/letsencrypt/live/realmlands.com-0002/privkey.pem; # Certificate address
# Fixed Writing
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
# Projects with reverse proxy configuration here
location /forums {
proxy_pass https://localhost:8080/forums;
# Fixed Writing
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
location / {
proxy_pass https://localhost:8443;
# Fixed Writing
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
But what happens when i go to url/forums it seems to redirect the browser to localhost/forums and shows the "This can't be reached".
How can i make it redirect to the nginx forums directory?

Tell lighttpd used protocol (HTTPS) when Nginx reverse proxy is in front

I have a Nginx reverse proxy redirecting to a lighttpd server in the same machine. This reverse proxy works on HTTPS so I want to tell to lighttpd that HTTPS is being used as protocol instead of HTTP. Here is my Nginx confuguration.
server {
server_name mydomain.com;
merge_slashes off;
rewrite ^(.*?)//+(.*?)$ $1/$2 permanent;
location / {
proxy_pass http://localhost:8088/;
proxy_set_header Host $host;
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 https;
proxy_set_header X-Forwarded-Ssl on;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
# SSL settings
}
server {
if ($host = mydomain.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
}
The lighttpd server is running a python application that uses web.py module but the returned value by web.ctx.protocol still is HTTP when it should be HTTPS. It looks like lighttpd ignores the X-Forwarded-Proto header sent by Nginx.
What am I doing wrong? Is there any additional configuration to be done?
Thanks.
You have to configure lighttpd to trust headers from upstream. Use mod_extforward in lighttpd. See https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModExtForward
Better than your many headers above, both nginx and lighttpd (via mod_extforward) support RFC 7239 Forwarded header.
https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/
Use of the "Forwarded" header should be preferred.

Laravel Forge Nginx redirecting all subdomains to https://example.com

So I'm using Laravel Forge's built-in features to generate and integrate a LetsEncrypt certificate. But I'm having issues with getting my subdomains to work.
Whenever I try to go to www.example.com or https://www.example.com, it redirects me to https://example.com. The same happens when I try to add more subdomains, like foo.example.com redirects to https://example.com
I have my domain hosted with Namecheap and set it up as so:
This is my Nginx configuration file as generated by Forge:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/before/*;
# FORGE CONFIG (DOT NOT REMOVE!)
include upstreams/example.com;
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/244866/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/244866/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
charset utf-8;
access_log off;
error_log /var/log/nginx/example.com-error.log error;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
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;
proxy_pass http://386082_app/;
proxy_redirect off;
# Handle Web Socket Connections
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/after/*;
This is the first time I'm using an Nginx server so I'm not sure how to change that. I've tried playing around with my settings in Namecheap DNS settings, and making sure my SSL is generated with all the subdomains, but I don't the the issue is anywhere but within the nginx configuration. Searching around didn't give me a solid solution as I don't want to ward too far away from the default configuration by Forge, yet still manage to this work my way.
Any and all help will be highly appreciated!
Thank you

Resources