Nginx forward proxy with # in the url - regex

So I am trying to forward a url that contains a # and am having some difficulty. I have the following in the location sections:
location ~* /.%23/evl {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://foo.com;
}
location ~* /(?!.%23/evl) {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bar.com;
}
I would like everything with
http://example.com/#/evl/* to goto foo.com
everything else to bar.com
Any help would be appreciated.

Related

NGINX - Regex into locations.conf doesn't work

I have an issue into the configuration of my nginx proxy.
I just try to use regex has it is defined here: https://underthehood.meltwater.com/blog/2017/12/12/lightweight-tests-for-your-nginx-api-gateway/
But nothing works. My server won't start if it is any regex into the path.
I tried:
location ~ ^/tesla/(?<id>.*)$ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
}
or
location ~ ^/tesla/test/ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
}
or
location ~* ^/tesla/test/ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
}
Nothing work. My final aim is to extract a value from the url and do the following thing:
location ~* ^/(<version>.*)/test/ {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Prefix $version;
}
UPDATE (SOLVED)
I finally succeed thanks to Richard.
Here is the complete code:
location ~* ^/api/(?<version>.*)/(?<service>.*)(/.*/.*)$ {
proxy_pass http://$service.localnetwork:8080$3;
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-Prefix /api/$version/$service;
proxy_set_header msvc_name $service;
proxy_set_header msvc_version $version;
}
You can't use a proxy_pass with a static URI within a regular expression location block, it will throw an error - see your Nginx error log. See this document for details.
However, you can construct the URI to send upstream by appending variables to the proxy_pass statement. In your case, you could capture the remainder of the URI in the same regular expression.
For example:
location ~* ^/([^/]+)/test(/.*)$ {
proxy_pass http://localhost:8081$2;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Prefix $1;
}

Problems with nginx try_files setup

I'm trying to set up a project using django, gunicorn and nginx and I'm having trouble with the nginx configuration. More precisely when I use try_files.
If I use if (!-f $request_filename) {...} everything works fine but if use
try_files ... Django generates the exception:
Invalid HTTP_HOST header: 'myproject_server'. The domain name provided is not valid according to RFC 1034/1035.
Once everything works using the if ... I assume that the other settings
(gunicorn etc) are correct.
The configuration files I'm using are:
/home/myproject/myproject/settings.py (django)
...
ALLOWED_HOSTS = [192.168.200.100, ]
...
/etc/nginx/sites-available/myproject (this one WORKS)
upstream myproject_server {
unix server:/home/myproject/run/gunicorn.sock fail_timeout = 0;
}
server {
listen 80;
server_name 192.168.200.100;
root /home/myproject;
location /media/ {}
location /static/ {}
location / {
proxy_set_header Host $HTTP_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 $scheme;
if (!-f $request_filename) {
proxy_pass http://myproject_server;
break;
}
}
}
/etc/nginx/sites-available/myproject (this one DOES NOT WORK)
upstream myproject_server {
unix server: /home/myproject/run/gunicorn.sock fail_timeout = 0;
}
server {
listen 80;
server_name 192.168.200.100;
root /home/myproject;
location /media/ {}
location /static/ {}
location / {
proxy_set_header Host $HTTP_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 $scheme;
try_files $uri #myproject_backend;
}
location #myproject_backend {
proxy_pass $scheme://myproject_server;
}
}
What am I doing wrong?
Thanks in advance any help.
PS: English is not my native language so I apologize for the (many) errors.
proxy_set_header should be in the same location as proxy_pass.
location / {
try_files $uri #myproject_backend;
}
location #myproject_backend {
proxy_set_header Host $HTTP_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 $scheme;
proxy_pass http://myproject_server;
}

Nginx proxy - limit request with regex

I've an Nginx 1.6.2, working as proxy. The backend HTTP server is an Apache.
I'ld like to control the number of connections certain URL, in some vhosts. The exact "location" form is works, but if I pass a regex to "location", it doesn't.
server {
listen 80;
server_name www.myhost.com;
location ~* ^/.*ABCD_promo.*$ {
limit_req zone=one burst=5;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://vm-apache4;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://vm-apache4;
}
}
In nginx.conf, I've this directive:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
before the config part(s) above.
When I change the "location ~* ^/.*ABCD..." to "location /ABCD_promo", then it works. What em I misses?

Nginx: location regex not working

i'm getting trouble with my regex don't know what is wrong with it. it's returning a URi with orls/f instead of orls/f?p=4550. When i pass https://secure.toto02.com/orls/myservice/f?p=4550
my conf file is below
location ~ "^/([a-zA-Z]+)/myservice/(.+)$" {
error_log /var/log/nginx/error-server.log notice;
rewrite_log on;
#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 $scheme;
proxy_pass http://192.168.3.45:8080/orls/$2;
proxy_redirect http://192.168.3.45:8080/orls/ https://secure.toto02.com/$1/myservice/ ;
}
Can anyone help?
Capturing the location will not encompass the query string. You have to manually include it via $is_args and $args variables like so:
proxy_pass http://192.168.3.45:8080/orls/$2$is_args$args;

Rewrite URL for reverse proxy

This is similar to nginx url rewrite for reverse proxy, but I don't know how I should apply that answer to my situation.
I am trying rewrite URLs for a reverse proxy, so that /pathA/something becomes /pathB/something when Nginx makes the request to the upstream server.
location ~ /pathA(/|$) {
proxy_pass http://www.example.com;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
rewrite ^/pathA(.*) /pathB$1;
}
I tried this, but I get an Nginx 404 page (and not the example.com 404 page).
I needed to add redirect (302) or permanent (301):
location ~ /pathA(/|$) {
proxy_pass http://www.example.com;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
rewrite ^/pathA(.*) /pathB$1 permanent;
}