Home > Enterprise >  NGINX location with and without extension to proxy_pass
NGINX location with and without extension to proxy_pass

Time:07-07

I'm learning nginx rules.

I have been reading about the location module: http://nginx.org/en/docs/http/ngx_http_core_module.html#location

I would like to catch and proxy_pass either if the user types the path with or without extension.

/user and /user.php both should proxy to mytestingsite.co/user.php

I tried this but it just work for /user.php

location ~ /user {
  proxy_ssl_server_name on;
  proxy_pass "mytestingsite.co/user.php";
}

CodePudding user response:

Location you've shown in your question won't work at all. Besides you do not specify the access scheme (http or https), you can't specify an URI after the upstream name inside the regex location, this configuration will give you the following error:

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in ...

Regex you are using will match anything with the /user substring, e.g. /api/user/ or /username.

If you want to match /user and /user.php URIs only, and pass the /user.php URI to the upstream server, you can use the following location:

location ~ ^/user(\.php)?$ {
    rewrite ^ /user.php break;
    proxy_ssl_server_name on;
    proxy_pass https://mytestingsite.co;
}
  • Related