Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

classic Classic list List threaded Threaded
11 messages Options
xrd
Reply | Threaded
Open this post in threaded view
|

Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

xrd
I have the following server configuration.  I have two problems that I would
appreciate if someone can tell me what I am doing wrong:

1- this location is not respected since the configuration lowercases all
uris without respecting these locations.  Please note that api,
contentAssets, categoryServlet are paths, not directories or folders.

location ~
^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA)/{}


2.  It seems that if I am in a reverse proxy I have to input the proxy
definition in every location because otherwise it does not work.  Is the a
more practical way to do this?


Thank you for all your help!

Lex

Note: I have changed the proxy IP address and port for x and Is.





server {

        listen 80;
 
        server_name example.com;
##Trailing Slash in URLContent
## It doest not work on http://example.com/real-estate/cccc/
#rewrite ^/(.*)/(.*)/$ /$1/$2 permanent;

 # matches any query beginning with /contentAsset or dA/ and halts
searching,
# so regular expressions will not be checked.
location ~
^/(api|contentAsset|categoriesServlet|dotAdmin|DotAjaxDirector|html|dwr|dA)/
{

            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

                }
 ## IT MUST BE FIRST TO LOWERCASE THE URI FIRST
      ## IF IT HAS UPPERCASE CHARACTERS
      location ~ [A-Z] {
          return 301 $scheme://$host$my_uri_to_lowercase;


            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

                }

 ## IT MUST BE FIRST TO LOWERCASE THE URI FIRST
      ## IF IT HAS UPPERCASE CHARACTERS
      location ~ [A-Z] {
          return 301 $scheme://$host$my_uri_to_lowercase;
     
}
         location / {
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

                    }


        }

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285780,285780#msg-285780

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

Francis Daly
On Thu, Oct 03, 2019 at 11:36:27AM -0400, Alex Med wrote:

Hi there,

> 1- this location is not respected since the configuration lowercases all
> uris without respecting these locations.  Please note that api,
> contentAssets, categoryServlet are paths, not directories or folders.

Which part of the configuration does the lowercasing?

I don't see where $my_uri_to_lowercase is set.

> 2.  It seems that if I am in a reverse proxy I have to input the proxy
> definition in every location because otherwise it does not work.  Is the a
> more practical way to do this?

You need proxy_pass in each location{} where you want to reverse proxy
things.

But all of the other directives will inherit from the outer level (if
not set in the location).

> Thank you for all your help!

If the problem remains, can you show a request that you make, the response
that you get, and describe the response that you want instead?

It may help make clearer what is happening.

Thanks,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

xrd
Dear Francis:

Here are the answers to your questions.  Thank YOU for helping!

The location block are in the following order:

Location #1
location ~
^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA)/ {}

Location #2 ----> This location does the lowercasing
location ~ [A-Z] { return 301 $scheme://$host$my_uri_to_lowercase;}

Location #3
location /{}

Everything is lowercased.  So I want some paths to be untouched. So I
created location #1 to exclude some paths from lowercasing.  The problem is
that
in some URLs containing the paths above are lowercased.  For example:
/dwr/interface/CategoryAjax.js, /dwr/interface/HostAjax.js are lowercased.
Also, /html/images/languages/gh_TW.gif

Any help will be appreciated!



Note: The function definition for $my_uri_to_lowercase is inside my http
definition, here is a copy of it:
  ##
        ## URL REWRITE FUNCTION:  LOWERCASE ALL URIs
        ## START

        perl_set $my_uri_to_lowercase 'sub {
         my $r = shift;
         my $uri = $r->uri;
         $uri = lc($uri);
         return $uri;
         }';

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285780,285782#msg-285782

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

xrd
I also would like to add that when I access:  example.com/API/ it is
lowercased to example.com/api/.  According to my configuration it should
not.  So it means location #1 is not working properly.



Alex Med Wrote:
-------------------------------------------------------

> Dear Francis:
>
> Here are the answers to your questions.  Thank YOU for helping!
>
> The location block are in the following order:
>
> Location #1
> location ~
> ^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA)/
> {}
>
> Location #2 ----> This location does the lowercasing
> location ~ [A-Z] { return 301 $scheme://$host$my_uri_to_lowercase;}
>
> Location #3
> location /{}
>
> Everything is lowercased.  So I want some paths to be untouched. So I
> created location #1 to exclude some paths from lowercasing.  The
> problem is that
> in some URLs containing the paths above are lowercased.  For example:
> /dwr/interface/CategoryAjax.js, /dwr/interface/HostAjax.js are
> lowercased.
> Also, /html/images/languages/gh_TW.gif
>
> Any help will be appreciated!
>
>
>
> Note: The function definition for $my_uri_to_lowercase is inside my
> http definition, here is a copy of it:
>   ##
>         ## URL REWRITE FUNCTION:  LOWERCASE ALL URIs
>         ## START
>
>         perl_set $my_uri_to_lowercase 'sub {
>          my $r = shift;
>          my $uri = $r->uri;
>          $uri = lc($uri);
>          return $uri;
>          }';

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285780,285785#msg-285785

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

Francis Daly
In reply to this post by xrd
On Thu, Oct 03, 2019 at 12:21:29PM -0400, Alex Med wrote:

Hi there,

> Location #1
> location ~
> ^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA)/ {}
>
> Location #2 ----> This location does the lowercasing
> location ~ [A-Z] { return 301 $scheme://$host$my_uri_to_lowercase;}
>
> Location #3
> location /{}
>
> Everything is lowercased.  So I want some paths to be untouched. So I
> created location #1 to exclude some paths from lowercasing.  The problem is
> that
> in some URLs containing the paths above are lowercased.  For example:
> /dwr/interface/CategoryAjax.js, /dwr/interface/HostAjax.js are lowercased.
> Also, /html/images/languages/gh_TW.gif

If I make my server{} have three location{}s:

==
location ~ ^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA)/ {
  return 200 "In the first location, uri $uri\n";
}

location ~ [A-Z] { return 200 "In the A-Z location, uri $uri\n"; }

location / { return 200 "In the / location, uri $uri\n"; }
==

then the requests for /dwr/interface/CategoryAjax.js,
/dwr/interface/HostAjax.js, and /html/images/languages/gh_TW.gif are
served from the first location, not the second.

So - I'm not seeing what you are reporting.

When you do a "curl -i" for /dwr/interface/CategoryAjax.js, what response
do you get?

HTTP 200, HTTP 301, something else?

Thanks,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

Francis Daly
In reply to this post by xrd
On Thu, Oct 03, 2019 at 01:03:01PM -0400, Alex Med wrote:

Hi there,

> I also would like to add that when I access:  example.com/API/ it is
> lowercased to example.com/api/.  According to my configuration it should
> not.  So it means location #1 is not working properly.

Why do you think that?

My reading of your configuration is that a request for example.com/API/
should get a response of a http 301 to example.com/api/.

Is there some part of your configuration that you are not showing here?

If you test with exactly the configuration that you show here, do you
see the same behaviour?

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

xrd
Dear Francis:

Thank you again for your help!

I apologize. Perhaps, I should have started with what I want to accomplish
with the configuration.  
I want the server to lowercased every URI except the ones that begins with
any of the following
paths:

/api/
/contentAsset/
/categoriesServlet/
/DotAjaxDirector/
/html/
/dwr/
/dA/
/JsonTag/

Most of the above paths belong to a protected area that requires the user to
login with an id and a password.

So for example, if the server receives a request for
https://example.com/API/ or https://example.com/contentAsset/
or https://example.com/DotAjaxDirector/ it should leave them intact. This is
not happening in certain
instances

For anything else, it should lowercase any other uri.


Now, to answer your questions:

Your Question # 1
-----
then the requests for /dwr/interface/CategoryAjax.js,
/dwr/interface/HostAjax.js, and /html/images/languages/gh_TW.gif are
served from the first location, not the second.

So - I'm not seeing what you are reporting.

When you do a "curl -i" for /dwr/interface/CategoryAjax.js, what response
do you get?

HTTP 200, HTTP 301, something else?
---

Answer to Your Question:

I get a 401 error not authorized if I use curl.  But if I go and log in the
restricted area via a web browser and viewed the page with the inspertor's
developer tool I no longer see some of the error on the console.  This is
probably because I was getting
the errors from cache? I cleared the cache this morning so I stopped seeing
some errors.  I still see a:
Failed to load resource:the server responded with a status of 404() to the
following urls:
example.com/jsontags
example.comcategoriesservlet

The errors are being generated because nginx is lowercasing JsonTags and
CategoriesServlet.

If I do a curl:

curl -i http://example.com/JsonTags
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 04 Oct 2019 19:21:32 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://example.com/jsontags

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

curl -i http://example.com/CategoriesServlet
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 04 Oct 2019 19:23:56 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://example.com/categoriesservlet

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>



For URIs outside of the restricted areas:

If I write http://example.com/API/ or http://example.com/API they are
lowercased, they should not be lowercase because it
should be served by location #1 which makes the path case insensitive with
the "~".

curl -i http://example.com/API/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 04 Oct 2019 19:24:56 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://example.com/api/

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>



However, the url http://example.com/contentAsset is not lowercased and this
uri is outside of the restricted area.

curl -I
http://example.com/contentAsset/image/ctfgs9b9b-fdcb-4a4a-807d-123x896fb0/fileAsset/filter/Resize,Jpeg/resize_w/1000/Jpeg_q/.8
HTTP/1.1 200
Server: nginx
Date: Fri, 04 Oct 2019 19:27:10 GMT
Content-Type: image/jpeg
Content-Length: 83580
Connection: keep-alive
Content-Disposition: inline; filename="scarlet-macaw-ara-macao.jpg"
Expires: Sat, 03 Oct 2020 19:27:10 +0000
Cache-Control: public, max-age=31536000
Last-Modified: Sat, 28 Sep 2019 17:49:34 +0000
ETag: dot:ctfgs9b9b-fdcb-4a4a-807d-123x896fb0:1569692974000:83580
Accept-Ranges: bytes


I also get this error:  WebSocket connection to
'ws://example.com/api/ws/v1/system/events' failed: Unexpected response code:
200. This I have been told
that I have to set the proxy pass to localhost rather than IP in order to
work it properly.  I have not tested it yet.

Below is the current configuration for your consideration. Any ideas what I
could be doing wrong?




        # SERVER DEFINITIONS
##BETA EXAMPLE HTTP SERVER CONFIGURATION START
server {

        listen 80;
   
        server_name example.com;

        ## URL REWRITE FUNCTION THAT LOWERCASES ALL URIS
        ## START
       ## THIS IS OUTSIDE OF THE SERVER DEFINITION IN THE HTTP BLOCK - I PUT
IT HERE SO YOU CAN SEE IT

        perl_set $my_uri_to_lowercase 'sub {
         my $r = shift;
         my $uri = $r->uri;
         $uri = lc($uri);
         return $uri;
         }';
        ## URL REWRITE FUNCTION THAT LOWERCASES ALL URIS
        ## END

##Trailing Slash in URLContent
## It doest not work on http://beta.example.com/real-estate/properties
#rewrite ^/(.*)/(.*)/$ /$1/$2 permanent;



## LOCATION #1
## THIS LOCATION WILL PREVENT LOWERCASING IN ANY URI THAT BEGINS WITH
/API/WHATEVER , /CONTENTASSET/WHATEVER, ETC.
## ~ MAKES IT CASEINSENSITIVE SO EITHER /api/ or /API/ will not be
lowercased
## ^ MAKES IT MATCH WITH THE BEGINING OF THE "/" AND THE PATH WE DO NOT WANT
TO LOWERCASE
## I ADDED THE proxy_pass as observation of David Francis on Nginx Forum
## IF URI MATCHES THIS LOCATION THEN IT WILL STOP SEARCHING FOR OTHER
LOCATION MATCHES

location ~
^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA|JsonTags)/
{
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

                }
      ## LOCATION #2
      ## THIS LOCATION WILL LOWERCASE ANYTHING THAT HAS UPPCASE LETTERS
      ## EXCEPT THE PATHS STATED ON LOCATION #1
      location ~ [A-Z] {

            return 301 $scheme://$host$my_uri_to_lowercase;
     
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;
}

      ## LOCATION #3
      ## THIS LOCATION IS THE DEFAULT LOCATION
         location / {
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

                    }


        }
##BETA EXAMPLE HTTP SERVER CONFIGURATION END

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285780,285797#msg-285797

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

Francis Daly
On Fri, Oct 04, 2019 at 03:36:13PM -0400, Alex Med wrote:

Hi there,

Thanks for the extra information.

There are a few different things in here, but there is one that stands
out as probably being a "quick fix" that should allow the rest to be
tested separately...

> Your Question # 1
> -----
> then the requests for /dwr/interface/CategoryAjax.js,
> /dwr/interface/HostAjax.js, and /html/images/languages/gh_TW.gif are
> served from the first location, not the second.
>
> So - I'm not seeing what you are reporting.
>
> When you do a "curl -i" for /dwr/interface/CategoryAjax.js, what response
> do you get?
>
> HTTP 200, HTTP 301, something else?
> ---
>
> Answer to Your Question:
>
> I get a 401 error not authorized if I use curl.

Ok; that suggests that it *is* being handled in your location#1, which
is what is wanted. That's good.

> The errors are being generated because nginx is lowercasing JsonTags and
> CategoriesServlet.
>
> If I do a curl:
>
> curl -i http://example.com/JsonTags
> HTTP/1.1 301 Moved Permanently
> Server: nginx
> Date: Fri, 04 Oct 2019 19:21:32 GMT
> Content-Type: text/html
> Content-Length: 178
> Connection: keep-alive
> Location: http://example.com/jsontags

Yes - that is because that's what your config asks it to do. Because...

> If I write http://example.com/API/ or http://example.com/API they are
> lowercased, they should not be lowercase because it
> should be served by location #1 which makes the path case insensitive with
> the "~".

No.

"~" means "this is a regex location".

"~*" would be "this is a case-insensitive regex location".

http://nginx.org/r/location

If you change your config to use "~*", does that make things do what
you want?

(Note: your location#1 will not match "/API", because that regex location
needs two slashes. So /API will probably be lowercased to /api, and /api
will probably be handled in location#3.)

> I also get this error:  WebSocket connection to
> 'ws://example.com/api/ws/v1/system/events' failed: Unexpected response code:
> 200. This I have been told
> that I have to set the proxy pass to localhost rather than IP in order to
> work it properly.  I have not tested it yet.

Websockets have not been mentioned in this mail yet, as far as I know.

I suggest: get the rest working first; then sort out websockets.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

xrd
Dear David:

I am very grateful for your help!  Below is the final configuration working
optimally.  I even included the proxy configuration parameters for the
websocket connection to work.I noticed that the location =/  does not
support OR  so I had create two additional locations to catch paths without
the end forward slash.   Is there a limit in the number of location that a
configuration file should have?

I do have some other additional configuration I want to do, but I will post
another message with it.

Thank YOU again for being in this forum and helping everyone!

Regards,

Alex

        # SERVER DEFINITIONS
##BETA EXAMPLE HTTP SERVER CONFIGURATION START
server {

        listen 80;
   
        server_name example.com;

        ## URL REWRITE FUNCTION THAT LOWERCASES ALL URIS
        ## START -- THIS IS INSIDE THE HTTP, BUT IT IS HERE FOR ILLUSTRATION
PURPOSES

        perl_set $my_uri_to_lowercase 'sub {
         my $r = shift;
         my $uri = $r->uri;
         $uri = lc($uri);
         return $uri;
         }';
        ## URL REWRITE FUNCTION THAT LOWERCASES ALL URIS
        ## END





## LOCATION #1
## THIS LOCATION WILL PREVENT LOWERCASING IN ANY URI THAT BEGINS WITH
/API/WHATEVER , /CONTENTASSET/WHATEVER, ETC.
## ~ MEANS IT IS  A REGEX
## ~* MAKES IT CASEINSENSITIVE SO EITHER /api/ or /API/ will not be
lowercased
## ^ MAKES IT MATCH WITH THE BEGINING OF THE "/" AND THE PATH WE DO NOT WANT
TO LOWERCASE
## I ADDED THE proxy_pass as observation of David Francis on Nginx Forum
## IF URI MATCHES THIS LOCATION THEN IT WILL STOP SEARCHING FOR OTHER
LOCATION MATCHES

location ~
^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA|JsonTags)/
{
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

            ###
            ### ADD THIS FOR WEBSOCKET SUPPORT
            ###

               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "Upgrade";

                }


## LOCATION #2
## THIS LOCATION WILL CATCH THE NECESSARY STRINGS WITHOUT A FORWARD SLASH
## SO THAT THE BACKEND WORKS PROPERTY

location = /categoriesServlet {
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

            ###
            ### ADD THIS FOR WEBSOCKET SUPPORT
            ###

               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "Upgrade";

    }

##LOCATION #3
## HANDLES ONE PATH WITHOUT FOWARD SLASH
##

location = /JSONTags {
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

            ###
            ### ADD THIS FOR WEBSOCKET SUPPORT
            ###

               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "Upgrade";

    }


      ## LOCATION #4
      ## THIS LOCATION WILL LOWERCASE ANYTHING THAT HAS UPPCASE LETTERS
      ## EXCEPT THE PATHS STATED ON LOCATION #1
      location ~ [A-Z] {

            return 301 $scheme://$host$my_uri_to_lowercase;
     
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;
}

      ## LOCATION #5
      ## THIS LOCATION IS THE DEFAULT LOCATION
         location / {
            proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;
            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_connect_timeout 600;
            proxy_send_timeout 600;
            proxy_read_timeout 600;
            proxy_redirect off;

                    }


        }
##BETA EXAMPLE HTTP SERVER CONFIGURATION END

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285780,285799#msg-285799

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

xrd
Francis,

I apologized.  I wrote your name incorrectly in my previous message.  I am
not able to edit it.  

So, THANK YOU, FRANCIS!

Alex

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285780,285800#msg-285800

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

Francis Daly
In reply to this post by xrd
On Sat, Oct 05, 2019 at 12:44:30PM -0400, Alex Med wrote:

Hi there,

Good that you have a configuration that works for you.

If you want to spend more time on it, you may be able to remove some of
the duplication; but that is not necessary.

> I even included the proxy configuration parameters for the
> websocket connection to work.

Good stuff.

I would expect that the extra "websockets" configuration is not necessary
for non-websocket requests; so you might be able to add new location{}s
for just-websocket-requests -- but again, if what you have works, there
is no need to change anything.

> I noticed that the location =/  does not
> support OR  so I had create two additional locations to catch paths without
> the end forward slash.

Correct. "location =" is for an exact string match.

>  Is there a limit in the number of location that a
> configuration file should have?

"As many as it needs". If it needs more than your hardware can handle,
you need different hardware.

Which is just a way of saying: no, there is no practical limit that you
will meet.

> Thank YOU again for being in this forum and helping everyone!

You're welcome.

> location ~
> ^/(api|contentAsset|categoriesServlet|DotAjaxDirector|html|dwr|dA|JsonTags)/
> {

So - this location will match requests that start with "/X/" for each of
the eight strings in the pattern -- matching case-sensitively, because
you write "~" and not "~*".

>             proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

That line says to proxy_pass these requests elsewhere.

>             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_connect_timeout 600;
>             proxy_send_timeout 600;
>             proxy_read_timeout 600;
>             proxy_redirect off;

And those lines set some proxy-specific variables.

Because those same extra lines appear in many location{}s, you *could*
choose to put them at server{} level once, outside all location{}s, and
they would inherit into those location{}s, per the normal nginx method.

> location = /categoriesServlet {

This location will match exactly that request.

>             proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

That line is needed.

>             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_connect_timeout 600;
>             proxy_send_timeout 600;
>             proxy_read_timeout 600;
>             proxy_redirect off;
>
>             ###
>             ### ADD THIS FOR WEBSOCKET SUPPORT
>             ###
>
>                proxy_http_version 1.1;
>                proxy_set_header Upgrade $http_upgrade;
>                proxy_set_header Connection "Upgrade";

Those lines could be removed from here, if they were all set at server{}
level.

> location = /JSONTags {
>             proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

Same story here -- exactly one request handled, this capitalisation only.

>       location ~ [A-Z] {

This will match any request that includes a capital letter.

>             return 301 $scheme://$host$my_uri_to_lowercase;

And will return a redirect to the matching lowercased-request.

>             proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

This line (and the rest of them below) will not do anything, because the
"return" happens first.

>          location / {
>             proxy_pass <a href="http://xxx.xx.xx.xxx:IIII;">http://xxx.xx.xx.xxx:IIII;

This location will match any other requests, and will proxy_pass them
to upstream.

Cheers,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx