Nginx ReWrite location to New API endpoints - nginx

I am trying to move old API endpoints to new Restful (descriptive) endpoints. I have tried the below nginx configuration for rewriting old requests to the new endpoints but it is not working. Any help will be highly appreciated.
server {
listen 80;
root /path/to/api/entry/file;
index index.php;
server_name api.example.com;
#Below not rewriting http://api.example.com/create/ to http://api.example.com/users/v1/create
rewrite ^/create/ /users/v1/create last;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_pass unix:/var/run/php5-fpm.sock;
access_log /var/log/nginx/example_api-access.log;
error_log /var/log/nginx/example_api-error.log;
fastcgi_read_timeout 600;
}
}
An example to what am trying to achieve is rewrite http://api.example.com/create/ to http://api.example.com/users/v1/create and forward the request to the entry script (index.php) which will bootstrap the necessary controller to handle the request

Your rewrite...last doesn't achieve anything, as it's an internal process which eventually ends at /index.php. Your index.php script uses the original request (probably from the REQUEST_URI parameter) to determine the API endpoint.
You need to perform an external redirection using rewrite...permanent to make it visible to index.php. See this document for details.
For example:
rewrite ^/create/ /users/v1/create permanent;
Or more efficiently, and to work with POST and GET requests:
location /create/ { return 307 /users/v1/create$is_args$args; }
If you want to support the old API without a redirection, you will need to fool index.php with a dedicated location block, for example:
location /create/ {
include fastcgi_params;
fastcgi_param REQUEST_URI /users/v1/create;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Many of your fastcgi directives can be moved into the outer block, so that you do not need to write them twice. See this document for details.

Related

Nginx routing issue

Not entirely sure how to ask this but I have also looked all over the web and couldn't find an answer so any help is much appreciated.
I'm trying to set up an API call through my site that uses nginx, if I send the url /api/timestamp/ it works just fine and returns what is intended. However if i add a parameter and send /api/timestamp/2015-08-09 it tries to open the file 2015-08-09 which obviously doesn't exist.
How do I get Nginx to pass the parameter as an argument to my program and not try to use it as a route? Or am I looking at this all wrong?
server {
listen 83 default_server;
server_name portfolio.com;
access_log /var/log/nginx/port.access.log;
error_log /var/log/nginx/port.error.log;
root /var/www/portfolio/;
index index.php;
error_page 404 /404.html;
error_page 405 =200 $uri;
location /api/timestamp/ {
rewrite /api/timestamp/(.*) /api/timestamp/?param=$1 last;
}
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi.conf;
}
}
you need use a rewrite rule, the most simple solution for you is some like this
location /api/timestamp/ {
rewrite /api/timestamp/(.*) /api/timestamp/?param=$1 last;
}
Then you will receive the value in a parameter named param (change it according your requeriments).
Obviously, you can use a regex most restrictive to avoid undesired values .
You can get more information in https://www.nginx.com/blog/creating-nginx-rewrite-rules/.
Regards.

Nginx rewrite to prevent PHP file direct access

I'm trying to setup some rewrites so that
(1) .com/images/* will load naturally
(2) .com/* will be rewritten to .com/loader.php?control=*
However the code below works perfectly except it will execute .com/config.php instead of rewriting it to .com/loader.php?control=config.php
How can I prevent my rewrite being overridden? I only want .php files to be executed if it's loader.php or in the images folder. (Been trying for hours)
server {
listen 80;
root /mnt/web/test_com/public_html;
server_name test.com;
index index.html index.php index.htm;
location ~ .php(.*)$ {
fastcgi_pass unix:/tmp/php-70-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $2;
include fcgi.conf;
}
location / {
rewrite ^/(.*)$ /loader.php?control=$1 last;
}
location /images/ {
}
}
Thanks!
You should look into the try_files directive
Like the return and rewrite directives, the try_files directive is placed in a server or location block. As parameters, it takes a list of one or more files and directories and a final URI
Read the whole blog-post written on the nginx blog here:
https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Nginx load subpath as wordpress root

I'm trying to set up a Wordpress in a system that has another php application installed, using nginx as web server.
I've simplified my config file to the maximun. The following confi is serving one post of my blog:
server {
listen 80;
server_name blog.ct.com;
root /home/ff/www/blog/;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_buffer_size 128k;
fastcgi_buffers 64 32k;
fastcgi_busy_buffers_size 128k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APPLICATION_ENV development;
fastcgi_param HTTP_X_FORWARDED_PROTO https;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
But, due my system's requirements I need to serve the blog from within a sub path (In my final system http://blog.ct.com/ should be serving my custom php app and http://blog.ct.com/vendor should be serving the wordpress blog).
The local root directory from wordpress must be /home/ff/www/blog/ (this cannot be changed, while my custom app's directory is /home/ff/www/myapp/). So I think I need to reserve location / for my custom app, I have to create a location /vendor
If I add /vendor and I return 403 in / (just to debug easier), the browser says 405 (notice the =405 in /vendor, also to debug easier):
location /vendor {
try_files $uri $uri/ /index.php?$uri&$args =405;
}
location / {
return 403;
}
So I think nginx is going into location /vendor but is not finding my php script in /home/ff/www/blog/index.php so its returning the fallback 405.
Any idea why this could happen?
How can I achieve to load http://blog.ct.com/vendor as the root from wordpress but keeping http://blog.ct.com/ using another php script?
I've found out the following hints that gave me the clue to fix the problem (in case someone has the same problem than me, this may help)
Using location /path is not the same as using location ~(/path) (regex have different priority, so maybe they are not being checked in the order you think)
Adding error_log /your/path/log/error.log debug; to any location block may help you to see how is nginx serving every request (e.g. to location fastcgi, location \vendor, or the server{ block).
alias /var/www/path/vendor works different than root /var/www/path/vendor (check Nginx -- static file serving confusion with root & alias);
In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.
using rewrite with alias can help you parse the php file you want independent of the path
if (!-f $request_filename) {
rewrite ^ $document_root/index-wp.php last;
}
Take care of the SCRIPT_FILENAME you are using (check it with error_log, see above), maybe you need fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; but you are loading fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; so depending on your previous config you may be attaching the document root twice.
Two different configurations for fastcgi can be used if you change your index.php file names. E.g. location ~ wp\.php$ { will work with wp.php while location ~ \.php$ { will work with all other php files like index.php.

Moodle 2.3 with Nginx vs slash argument rewrite

I'm trying to setup Moodle 2.3 (not 2.5) ver with nginx latest build. There was some advice on this site before. One of them: Moodle 2.0 with Nginx backend.
Apparently as anybody knows, Moodle is using path_info rules to post URL's like this: http://example.com/moodle/pluginfile.php/26/mod_scorm/content/1/index.html. To escape all this nightmare, Moodle is offering to disable "Slash arguments" in UI. Which is great. But not for SCORM player which is forcing "Slash argument" despite the previous option. So with disabled "Slash arguments" everything is working and normal. But my only goal is to use SCORM player.
I tried to use the rewrite rule from the link above:
rewrite ^(.*\.php)(/)(.*)$ $1?file=/$3 last;
which is not working in 2.3-2.5 ver. I assume it worked in 1.9.
Now Moodle is using different path:
http://example.com/moodle/pluginfile.php/26/mod_scorm/content/1/index.html
Some of nginx rules:
location ^~ /moodle {
location ~* ^.+\.(?:css|js|htc|xml|jpe?g|gif|png|ico|bmp|svg|swf|pdf|docx?|xlsx?|tiff?|txt|rtf|cgi|bat|pl|dll|aspx?|class|otf|ttf|woff|eot|less)$ {
add_header Access-Control-Allow-Origin *;
access_log off;
expires 30d;
tcp_nodelay off;
try_files $uri =404;
}
location ~* ^/moodle/.*\.php$ {
include includes/fastcgi_params.conf;
try_files $uri #dynamic;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_read_timeout 1200;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9090;
}
rewrite (^.*\.php)(/.*) $1 last;
}
Please advise how to solve this.
(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I solved this by putting rewrite directive in {server} not in {location} section. In my scenario moodle is installed under subfolder: example.com/moodle.
server {
server_name example.com www.example.com;
rewrite ^/moodle/(.*\.php)(/)(.*)$ /moodle/$1?file=/$3 last;
location ^~ /moodle {
try_files $uri $uri/ /index.php?q=$request_uri;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9090;
include includes/fastcgi_params.conf;
}
}
}

nginx rewrite mystery - duplicating hostname and losing https

I am replacing lighttpd with nginx on my development server. I got it working with PHP and SSL, but I'm stumped by what should be a simple rewrite. I need to rewrite URLs from
http[s]://dev.foo.com/signup/123456
to
http[s]://dev.foo.com/signup/index.php?attcode=123456
The rule I am using is:
rewrite ^/signup/([0-9]+)$ /signup/index.php?attycode=$1 last;
I have tried numerous variations on this, moved it around, put it inside a location block. What happens is the URL is rewritten to:
http://dev.foo.com/dev.foo.com/signup/123456
The hostname is inserted, and it seems to always lose https and go to http.
My nginx.com server section is below. I have read and re-read the nginx docs (as they are) and searched the nginx mailing list, but nothing I've tried has solved this problem.
Ubuntu 8.0.4 LTS in case that matters.
Thanks.
server {
listen 80;
listen 443 default ssl;
server_name dev.foo.com dev.bar.com localhost;
root /var/www/foo;
index index.php index.html;
# ssl cert stuff omitted
charset utf-8;
access_log /var/log/www/dev.access.log main;
location ~ /\. {
deny all;
}
location ~* ^.+\.(inc|tpl|sql|ini|bak|sh|cgi)$ {
deny all;
}
location ~* ^/(scripts|tmp|sql)/ {
deny all;
}
rewrite ^/robots.txt$ /robots_nocrawl.txt break;
rewrite ^/signup/([0-9]+)$ /signup/index.php?attycode=$1 last;
location / {
try_files $uri $uri/ /error_404.php;
}
location ~ \.php$ {
fastcgi_pass localhost:51115;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SERVER_NAME $http_host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /error_404.php;
}
Don't put HTTP and HTTPS in the same server block. Separate them into two almost-identical server blocks, one for HTTP and one for HTTPS. Otherwise you will confuse all kinds of Nginx internals.

Resources