Nginx Reverse Proxy Lowercase URL and some exceptions to the URL

Alex Med nginx-forum at forum.nginx.org
Sat Oct 5 16:44:30 UTC 2019


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 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 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 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 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 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



More information about the nginx mailing list