Using proxy_set_header with proxy_redirect? - nginx

We have a nuxt app, and we need to have:
https://outer.domain.com/windows/thank-you
to be proxied to:
https://inner.domain.com/thank-you
I tried this:
server {
server_name outer.domain.com;
listen 80;
location /windows {
location /windows/ {
proxy_set_header Host inner.domain.com;
proxy_pass http://127.0.0.1:4300/;
}
location /windows/thank-you/ {
proxy_set_header Host inner.domain.com;
proxy_redirect http://$host/windows/thank-you/ /;
proxy_pass http://127.0.0.1:4300;
}
}
}#server block
But proxy_redirect seems not working, is there anything I am missing?

Related

Regex in nginx inside map

I cannot fix routing in nginx for different parts in URI . So if the request has URI starting with de it should pass the traffic to app_b. That does not happen and I'm getting error: invalid URL prefix in "http://"
Here's the config.
map $request_uri $resources_location {
"/" "app_a:1234/";
"^.*de.*$" "app_b:2345/";
}
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://$resources_location;
proxy_redirect off;
}
location ~ ^/(assets|public|favicon.ico) {
proxy_pass http://$resources_location;
}
}
How that can be solved (also with a help of map)?

Conditionally rewrite request url based on referer - Nginx

What I am trying to achive; is when the request coming from http://<ip>/vault/ui/ (referer in the request header) and it includes the http://<ip>/v1/* endpoint, to be rewriten or redirected to http://<ip>/vault/v1/
can someone please help me to solve this issue?
/etc/nginx/sites-enabled/reverse-proxy.conf
upstream command_center_vault {
server command-center-0.blinchik.io:28200;
}
server {
listen 80;
listen [::]:80;
location /vault/ {
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 Accept-Encoding "";
proxy_pass "http://command_center_vault/vault/";
proxy_redirect /ui/ /vault/ui/;
}
location /vault/v1/ {
proxy_pass "http://command_center_vault/v1/";
}
}
Headers
Update
A little bit more context, the overarching architecture looks as in the picture below.
the configuration of nginx server in the private subnet looks like this:
private subnet nginx
upstream consul_server {
server brain-consul-server-0.blinchik.io:8500;
server brain-consul-server-1.blinchik.io:8500;
server brain-consul-server-2.blinchik.io:8500;
}
upstream vault_server {
server brain-vault-server-0.blinchik.io:8200;
server brain-vault-server-1.blinchik.io:8200;
}
server {
listen 28500;
listen [::]:28500;
location /consul/ {
proxy_pass "http://consul_server";
sub_filter_once off;
sub_filter_types application/javascript text/html;
sub_filter "/v1/" "/consul_v1/";
}
location /consul_v1/ {
proxy_pass "http://consul_server/v1/";
}
}
server {
listen 28200;
listen [::]:28200;
location /vault/ {
proxy_pass "http://vault_server/";
port_in_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 Accept-Encoding "";
proxy_redirect /ui/ /vault/ui/;
sub_filter_once off;
sub_filter '<head>' '<head><base href="/vault/">';
sub_filter '"/ui/' '"ui/';
#inspired by this repo https://github.com/Folcky/hashicorp-vault-and-nginx
}
location /v1/ {
proxy_pass "http://vault_server/v1/";
}
}
public subnet nginx
upstream command_center_vault {
server command-center-0.blinchik.io:28200;
}
server {
listen 80;
listen [::]:80;
location /vault/ {
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 Accept-Encoding "";
proxy_pass "http://command_center_vault/vault/";
proxy_redirect /ui/ /vault/ui/;
}
location /vault/v1/ {
proxy_pass "http://command_center_vault/v1/";
}
}
the consul part works fine. if I change in the public subnet configuration the location of /vault/v1/ to /v1/ then it works as well. But the problem that other products that I intend to add it to the reverse proxy (like Nomad) also uses the /v1/ path and in this case there will be a conflict.
I think this one should work (must be placed at the server context outside any locations:
if ($http_referer ~ /vault/ui) {
rewrite ^/v1(/.*) /vault/v1$1 last;
}
You can make regex pattern more strict including //<ip> or https?://<ip> parts.

how to config nginx reverse proxy

I want to access to http://serverIP:9000/projects through my domain name. I tried to write the config file like this
server {
listen 80;
server_name xxx.xxx.com;
location / {
proxy_pass http://myserverip:9000/projects;
}
}
and this
server {
listen 80;
server_name xxx.xxx.com;
client_max_body_size 90m;
client_body_timeout 20m;
location / {
proxy_pass http://myserverip:9000/projects;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
but it still cannot access to http://myserverip:9000/projects. How should I write the config file to make it right. Thanks!
You can use:
proxy_redirect http://xxx.xxx.com http://myserverip:9000/projects;
proxy_redirect off;
server_name_in_redirect off;
but make sure CSS's path is right for this.
Or :
proxy_pass http://myserverip:9000/;
then you can access : http://xxx.xxx.com/projects

Possible to match location directive in nginx with a port number?

I have a config that looks like:
1)
server {
listen 80;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/project/static/;
}
}
2) updated
server {
listen 8080;
server_name mysite.com;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
root /home/www/flask-deploy/blog;
}
}
Updated issue:
If I view mysite.com:8080/static/css/main.css , i can see this, but if i visit mysite.com:8080, it will show the content for #1 not #2
The first configuration includes alias directive and the second consists root. So, that is the reason for the difference in their behaviour. Here are examples of matching URIs and real path in fs for both configurations:
1
location /static {
alias /home/www/flask-deploy/project/static/;
}
http://somehost:80/static/somefile -> /home/www/flask-deploy/project/static/somefile
http://somehost:80/static/dir/somefile -> /home/www/flask-deploy/project/static/dir/somefile
2
location /static/ {
root /home/www/flask-deploy/blog/static;
}
http://somehost:8080/static/somefile -> /home/www/flask-deploy/blog/static/static/somefile
http://somehost:8080/static/dir/somefile -> /home/www/flask-deploy/blog/static/static/dir/somefile

how to reverse proxy via nginx a specific url?

I have a server running at http://localhost:8080 i want a specific url of this server to be proxied by nginx.
For example, i only want http://localhost:8080/test/(.*) to be reverse proxied to http://localhost/test/(.*).
I'm proxing another server to http://localhost/.
What about just a simple location block?
server {
# ... other stuff
location /test/ {
try_files $uri #testproxy;
}
location #testproxy {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
# all your params
}
}
I made it somehow this way and it worked. Thanks for your comment anyway. :)
server {
listen 80;
# ... other stuff
upstream backend1 {
server 127.0.0.1:8080;
}
location /test/ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://backend1/test/;
}
}

Resources