PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] How to route the first URI segment using NGINX as a reverse-proxy?

 September 14, 2022     nginx, nginx-reverse-proxy, routes, segment, uri     No comments   

Issue

I'm trying to construct an architecture/infrastructure on Amazon Web Services. Today I have an EC2 working like a gateway, with NGINX on the background. Btw, I'm new with NGINX.

The last week I had this NGINX config file:

server {
    listen 80;

    # I put * for hide the real domain name
    server_name ******.com.ar www.******.com.ar;

    location / {
         proxy_set_header   X-Forwarded-For $remote_addr;
         proxy_set_header   Host $http_host;
         proxy_pass         "http://private.ip.1:80/";
    }
}

And it worked great! When I go to www.domain.com.ar, I get a redirect to private ip 1 on port 80.

But, nowdays, I need to adjust the config file a bit.

1) First, I need to redirect some known paths to private ip 1 (ex. /company, /portfolio, /services, /contact and subsequences: /company/ourvision, /services/software, /contact/workwithus). I'm using NodeJS, not PHP.

2) And if none of the before paths get a match, I need to get the first URI segment as a wildcard (ex. http://domain.com.ar/*) matching only this characters: A-z0-9 and send to private ip 2 on port 3000, also I need sending the wildcard word too (ex. http://private.ip.2:3000/wildcard-word)

I was only trying to success on my second point, but I couldn't deal with it.

server {
    listen 80;
    server_name ******.com.ar www.******.com.ar;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://private.ip.1:80/";
    }

    location ~ ^/(.*)/?$ { 
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://private.ip.2:3000/$1";
    }
}

But this isn't working. When I go to http://example.com.ar I go directly to the private ip 2 on port 3000. Btw, in another scenario, I get the follow error when I use the nginx -t command: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location

So, can anyone help this noob to solve his problem? Thanks in advance. I will let the links that I was reading below:

  • nginx rule for wildcard url
  • nginx location based on uri path
  • How to do URL Redirects with Nginx
  • How to rewrite URI nginx reverse proxy using proxy_pass?
  • Understanding Nginx Server and Location Blocks (DigitalOcean)

Solution

You want to use the location / { ... } block as your wildcard, as it matches any URI that will not match another location block.

You do not need a URI component in the proxy_pass statement, as you are not modifying the URI before sending it upstream. See this document for details.

You can save on typing by specifying the proxy_set_header statements within the outer block and allowing them to be inherited by both location blocks. See this document for details.

For example:

proxy_set_header   X-Forwarded-For $remote_addr;
proxy_set_header   Host $http_host;

location ~ ^/(company|portfolio|services|contact|)(/|$) {
    proxy_pass         "http://private.ip.1:80";
}

location / {
    proxy_pass         "http://private.ip.2:3000";
}


Answered By - Richard Smith
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing