I have a question here. I am developing a PHP SLIM framework application hosted for example on mydomain.com and one of my routes is grouped "api" like this:
$app->group(
'/api',
function () {
// All my API routes are here
}
);
I am able to access all my API routes by calling mydomain.com/api/{endpoint}. After deploying my application to production running Nginx, I was asked to convert all my URLs to: api.mydomain.com/{endpoint}. So I created a subdomain and used the code below that I have placed into my nginx configuration file:
location ^~ / {
rewrite ^/(.*$) mydomain.com/api/$1 last;
}
It is supposed to map any calls to api.mydomain.com to mydomain.com/api. However, it only works when an API call is one level for example, the following will work:
api.mydomain.com/resource1
api.mydomain.com/resource2
It will fail when using 2 levels, for example the following will fail. I get a "Method Not Allowed" error.
api.mydomain.com/resource1/inresource1
However when I call mydomain.com/api/resource1/inresource1 i get a successful request. I am not sure what I am doing wrong. I figure it has something to do with my rewrite script and the arguments. I am hoping someone can point me into the right direction.
Regards
The solution was pretty simple (works for me). All I had to do was change my location script to:
location / {
proxy_pass mydomain.com/api;
}
That way it forwards all requests (GET and POST) approriately.
Related
I'm trying to use Nginx Proxy Manager to add a query parameter to a path before passing it to a server using proxy_pass. The problem I'm having is that the query parameter doesn't seem to be added at all.
This is what I have added under "Custom Nginx Configuration":
location ~ /(sv|en)(.*) {
proxy_pass http://192.168.1.2:3000$2?lang=$1;
}
If I change proxy_pass to return 307, just to test if the regex is correct, I see the expected url in the browser. (for example ?lang=en) is added.
Based on what I have found online, this should work in regular nginx, but I have also found that Nginx Proxy Manager uses openresty. Could that cause any problems? Should I write it in some other way?
What I'm trying to accomplish is to be able to pass in a route such as this:
mysite.com/abc123/file.mp3
In Nginx I want it to read the abc123 and then call a piece of code (don't care what language: php, python, golang, fortran...) and then return the actual key that is needed to load the file.
In my config I have this:
server{
#lots of basic stuff here
location / {
mp4;
mp4_buffer_size 1m;
mp4_max_buffer_size 5m;
root /my_path/;
}
}
This works when I pass in my abc123/file.mp3. It will find the file and play it if that file exists in /my_path/abc123/file.mp3.
What I want is to translate (from a database) abc123 to myKey123 which would live at /my_path/myKey123/file.mp3
So, first, is this even possible?
If so, I'm not sure how to approach this. I know this question could have multiple solutions, but any direction will be appreciated.
The PHP script can issue a redirection using the X-Accel-Redirect header. This is treated by nginx as an internal redirect, so the rewritten URI is not exposed to the user. The documentation is a bit thin on details, hopefully you can find an example using PHP.
If you can ring-fence the rewritten URIs with a unique prefix, e.g. /download/myKey123/file.mp3, you can protect the files from direct access by using the internal directive. See this document for details. In which case, you will not need to obfuscate myKey123.
location /download/ {
internal;
...
}
for example I want anyone hits localhost/api to be redirected to a proxy of 127.0.0.1/api and whatever after it, for example, if I got localhost/api/getMyName then the config redirect it to 127.0.0.1/api/getMyName. or if someone hits localhost/api/getSomeone/1, it will proxy to 127.0.0.1/api/getSomeone/1
I tried something like
location /api {
proxy_pass http://127.0.0.1/api;
}
But the nginx just not responding at or, and adding /* or * after them just do not do the work... what should it actually be written to match the scenario I want above?
I'm new to nginx, and I'd like to know if there is a way to pass an edited request URL to a web server. For example, I may have a web server running (Let's say apache. Or Flask.). I want all requests to /foo/(.*)$ to go to my web server, but I want my web server to see them without the foo. But if the initial request didn't have the foo, it would go somewhere else completely.
A request to /foo/bar would be routed to my web server, which would see the request simply as "/bar". However, a request that originally was made to "/bar" would be handled differently entirely. (So I don't just want to redirect).
Is there any way to do this? My thinking is that I could use this to modularize and namespace applications I write, where I could write an application as if it were routing requests on root, but it would actually be buried deeper in my site.
Thanks!!
This should do the trick (untested):
location /foo {
rewrite /[^/](/.*) $1 break;
# Pass to web server, e.g.:
#proxy_pass http://127.0.0.1:8080;
}
Note that the regular expression within the rewrite will strip any sub-directory from the request; but only one sub-directory. You could also use:
rewrite /foo(/.*) $1 break;
Which is more readable and clearer but not as powerful. This is up to you and your regular expression skills.
We just moved to a new site, and want to redirect old links where necessary - however, some still work. For instance,
/holidays/sku.html
still works, while
/holidays/christmas/
no longer works. I'd like to be able to allow the site to attempt to serve a page, and when a 404 is reached, THEN try to pass it through a series of regex redirects, that may look like:
location ~* /holidays/(.*)+$ { set $args ""; rewrite ^ /holidays.html?r=1 redirect; }
I'm using a ~* location directive instead of doing a direct rewrite because we're moving from a Windows-based ASPX site to Magento with php-fpm behind nginx, so we suddenly have to worry about case sensitivity.
Without using nested location directives (which are actively discouraged by nginx documentation) with an #handler of some sort, what's the best way to allow nginx to attempt to serve the page first, THEN pass it across redirects if it fails?
Thanks!
http://wiki.nginx.org/NginxHttpCoreModule#try_files