nginx url rewrite for reverse proxy - nginx

I have an nginx on port 80 and a tomcat on port 8080 configured as upstream.
The war application in tomcat listen to /pwm.
I would like to configure nginx to a reverse proxy for tomcat and rewrite the url "/" to "/pwm".
example:
user types "web.noc.local" in browser and nginx rewrites the url to web.noc.local/pwm and redirects to tomcat on port 8080.
my nginx config:
upstream pwm_server {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80;
server_name web.noc.local;
access_log /var/log/nginx/log/web.noc.local.access.log main;
error_log /var/log/nginx/log/web.noc.local.error.log;
location / {
if ($is_args != "") {
rewrite "^$" /pwm break;
expires 7d;
proxy_pass http://pwm_server;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://pwm_server;
}
}
now when I open the url the, nothing happens, only a blank screen.
thx for help.

Ok, I found a solution for me:
location / {
rewrite ^ http://web.noc.local/pwm/ last;
}
location /pwm {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://pwm_server;
}

Related

Nginx returns 404 on proxy_pass

I have the following configuration on Nginx 1.20.1, whenever I try to access test.xxx.com/something/ I get a 404 error. I know there are other similar questions but I already have the / at the end of the proxy_pass so I have no idea what to do. The strange thing is that I have 20 other servers on that configuration, they are all identical but only this one doesn't work. Any idea?
upstream test {
server x.x.x.x:8443;
}
server {
listen 8090;
server_name test.xxx.com;
root /var/www/vhosts/test.xxx.com/;
location / {
access_log /var/log/nginx/access_test.log upstreamlog;
proxy_pass https://test/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 75s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
}
}

How to debug nginx reverse proxy?

In my use case, I am trying to do a reverse proxy using nginx server. I have two applications running in two ports. For example app server is running port 9090 and api server is running in 8081.
I will be running nginx server in port in 8080. If I get /api request, nginx should redirect to api server. Other requests should go to app server.
I have the following nginx.conf,
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream app {
server 127.0.0.1:9090;
keepalive 64;
}
upstream api {
server 127.0.0.1:8081;
keepalive 64;
}
#
# The default server
#
server {
listen 8080;
server_name howti;
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
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;
}
location /{
rewrite /(.*) /$1 break;
proxy_pass http://app;
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;
}
}
}
But it is not working. I am not able to debug the request in nginx? Could someone help me with this? Thanks

How to increase nginx timeout for upstream uWSGI server?

Stack used:
Nginx -> Uwsgi (proxy passed) -> Django
I have an API that takes aroundn 80 seconds to execute a query. Nginx closes the connection with the upstream server after 60 seconds. This was found in the nginx error log:
upstream prematurely closed connection while reading response header from upstream
The uWSGI and django application logs do not show anything weird.
This is my nginx configuration:
server {
listen 80;
server_name xxxx;
client_max_body_size 10M;
location / {
include uwsgi_params;
proxy_pass http://127.0.0.1:8000;
proxy_connect_timeout 10m;
proxy_send_timeout 10m;
proxy_read_timeout 10m;
proxy_buffer_size 64k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass_header Set-Cookie;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_ignore_headers Cache-Control Expires;
proxy_set_header Referer $http_referer;
proxy_set_header Host $host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
How do I increase the timeout, I have tried settings the proxy_pass timeout variables but they do no seem to be working.
Okay, so managed to solve this issue by replacing proxy_pass with uwsgi_pass
This is how my nginx conf looks now:
server {
listen 80;
server_name xxxxx;
client_max_body_size 4G;
location /static/ {
alias /home/rmn/workspace/mf-analytics/public/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_web.sock;
uwsgi_read_timeout 600;
}
}
And I had to set the socket parameter in my uwsgi ini file.
For some reason, the proxy_pass timeouts just wouldnt take effect.

ERR_TOO_MANY_REDIRECTS Nginx

I'm trying to redirect my particular domain to Tomcat where multipe Application is running, but I'm getting "ERR_TOO_MANY_REDIRECTS" ERROR in the browser
My configuration has below
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://localhost:7070/AppName;
proxy_read_timeout 600s;
client_max_body_size 200m;
}
}
Recently I configured my Odoo app to forward all requests via Nginx.
You need to add something like this to your Nginx config:
upstream tomcat {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://tomcat;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
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_read_timeout 600s;
client_max_body_size 200m;
}
}
If this doesn't work, for reference, you may want to check this article: https://www.rosehosting.com/blog/install-odoo-on-a-debian-8-vps-with-nginx-as-a-reverse-proxy/
I hope you'll find this useful.
It is common to set the proxy_redirect directive in the same way as the proxy_pass directive. see for example configure-nginx-with-proxy-pass.
location ~ ^/stash {
proxy_pass http://IP:7990;
proxy_redirect http://IP:7990/ /stash;
}
but I got the ERR_TOO_MANY_REDIRECTS error with this configuration... so i changed it for "proxy_redirect off;" as suggested here, and it solved my problem!
here is the configuration for my gitlab server:
server {
listen 80;
server_name reverseproxy.mydomain.org;
location /gitlab/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host-Real-IP $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.xx.xx.xxx:10080;
#proxy_redirect http://172.xx.xx.xxx:10080/ /gitlab/;
proxy_redirect off;
}
}
NB: i also needed to remove the directive "proxy_set_header Host $host;" for my gitlab server, powered by docker-gitlab.

Nginx proxy: rewrite rule for root request

I have nginx installed on port 80 and a node application on port 2368 behind nginx
nginx configuration looks like this
server {
server_name domain.com www.domain.com;
listen 80;
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
This configuration works exactly as expected. For example / request turns into http://localhost:2368/, and /post/mypost turns into http://localhost:1234/post/mypost etc.
What I want is that only / request turned into http://localhost:2368/latestpost/. And all other requests are handled the same way as in example above. Thnx!
You could use rewrite directive:
server {
server_name domain.com www.domain.com;
listen 80;
location / {
rewrite ^/$ /latestpost/ break;
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
or in separate location:
server {
server_name domain.com www.domain.com;
listen 80;
location = / {
rewrite ^.* /latestpost/;
}
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
Second variant is slightly more efficient as it will not try rewrite every request. But difference will be unnoticeable, I guess.

Resources