Nginx rewrite equivalent of apache rewrite - apache

I am trying to change the configuration on my new Nginx server so it matches my current Apache settings.
At the moment I am using this htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
I have found a converter that could "translate" this into the Nginx equivalent. I have tried the following:
location / {
if (!-e $request_filename){
rewrite ^/(.*) /index.php?url=$1 break;
}
try_files $uri $uri/ =404;
}
But when I try to set a url query like this http://domain.tld/something my php file gets returned and downloaded and that ain't supposed to happen.
What I expect to happen is when a url like http://domain.tld/something is entered it gets treated like http://domain.tld/index.php?url=something
Can someone tell me what I am doing wrong?

Unlike Apache, nginx doesn't come out-of-the-box ready to run PHP. You need to setup a handler to deal with php files, otherwise nginx will serve them up just like any regular file.
See: Nginx downloads php instead of running it
Also: review the NGINX documentation on using fast-cgi to run php.

Related

File Manager of CKEditor WolfCMS not working with Nginx

I migrated a WolfCMS from Apache to Nginx. Everything is working fine except for the CKEditor File Manager plugin. When opening the browser server box of CKEditor I am seeing No input file specified. All other friendly URLs, rewrites are working without any issues. So I assume the issue is related to a wrong Nginx configuration.
These are the Apache rewrites:
RewriteEngine On
# Set next line to your Wolf CMS root - if not in subdir, then just /
#RewriteBase /wolfcms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Main URL rewriting.
RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]
For Nginx the docs just state:
#Put the following code in your server block:
try_files $uri $uri/ /index.php?WOLFPAGE=$uri&$args;
And that is what I did. The file manager URL that is not working in Nginx is:
/wolf_ckeditor/filemanager/index.php?type=Images&CKEditor=part_0_content&CKEditorFuncNum=1&langCode=de
=> Leading to No input file specified.
Any suggestions on how I could modify my Nginx config to get this working again?

How to rewrite nginx to execute a php file before serving static content?

I am trying to do this in nginx but I just cannot get it to work:
# protect images
RewriteCond %{REQUEST_FILENAME} \.(bmp|gif|ico|jpe|jpeg|jpg|png)$
RewriteRule .* /path-to-file.php [L,QSA]
Tried it with try_files and rewrite, any advice is appreciated!
To rewrite all image URIs to a specific PHP script, you could use a regex location and an internal rewrite:
location ~* \.(bmp|gif|ico|jpe|jpeg|jpg|png)$ {
rewrite ^ /path/to/file.php;
}

Convert htaccess rules routing paths to index.php to nginx

I'm new to nginx, but I'm trying to convert
# Disallows file browsing
Options -Indexes
# Custom Error Pages
ErrorDocument 403 /error-docs/403.php
ErrorDocument 404 /error-docs/404.php
ErrorDocument 500 /error-docs/500.php
# Turn on the Apache Rewrite Engine
RewriteEngine On
#Conditional to see if it is a real file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If not then pass the query string to index.php and stop any further rules
RewriteRule (.*) index.php/$1 [PT,L]
to nginx syntax. I've tried the suggestion at Routing requests through index.php with nginx but it doesn't do quite what I need it to do. I've also tried the auto converters http://winginx.com/en/htaccess and http://www.anilcetin.com/ but the rules they output don't work for me either. Any help is appreciated.
The issue I had was actually related to my PHP and MySQL code.
location / {
try_files $uri $uri/ /index.php$args;
}
works fine. Thanks for the link.

Apache mod_rewrite to Nginx rewrite rules

My site is running on Nginx and I am trying to add a software in the sub-directory of the site that uses Apache's mod_rewrite rules. E.g. www.mydomain.com/mySubfolder
Here is the Apache .htaccess
#Options -Indexes
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
</ifModule>
So far I managed to get the main page to work but when requesting the login page, it is causing a URL redirect loop. E.g. www.myDomain.com/login
With this:
location /mySubfolder {
if (!-e $request_filename) {
rewrite ^(.*)$ /mySubfolder/index.php?q=$1 last;
break;
}
}
I have been reading and trying to learn how to convert Apache to Nginx and even used the .htaccess to Nginx converter I found at http://winginx.com/htaccess but the tool doesn't seem to recognize the %{REQUEST_URI} ^/system.* part. Upon my research and study, I came up with:
location /mySubfolder {
if ($request_uri ~ "^/(system.*)$") {
rewrite ^/(.*)$ index.php?/$1 last;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /mySubfolder/index.php?q=$1 last;
break;
}
}
I am a complete noob at this and was even wondering if I am even close to accomplish this conversion to work. Please help.
Thank you.
was even wondering if I am even close to accomplish this conversion to
work
You've basically taken the wrong approach to use in Nginx.
Although it is kind of natural to assume that the rewrite rules in Apache would be mapped to the rewrite in Nginx, they aren't. Instead they are mostly mapped to the location rules.
Your block for mySubfolder should look like:
location ^/mySubfolder {
try_files /mySubfolder/index.php?$args =404;
}
You aren't actually rewriting anything - you are just telling nginx that any requests that start with /MySubfolder should be served by the files listed in try_files. btw you have to tell Nginx to pass the query string through which is what the args is doing.
You can append the original URL (i think) though it may be easier just to use $_SERVER['REQUEST_URI'] inside your script.
I believe the rewrite rule you have to the same start URI is causing the /mySubFolder to keep matching.
In Nginx rewriting is only used when you want to make external URL paths be served by different internal urls, and normally the rewrite rule is not inside a location block.
For example I have a file server, that serves up images and other files. I have these rewrite rules in the server block:
rewrite ^/image/(\d+)/(\w+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&mode=$2&imagePath=$3.$4&resizeAllowed=TRUE&type=image last;
rewrite ^/image/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&imagePath=$2.$3&resizeAllowed=TRUE last;
rewrite ^/file/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyFile.php?typeID=$1&imagePath=$2.$3&resizeAllowed=FALSE last;
to make the external URLs look nice. But then they are all served by the location block:
location ~* ^/proxy {
try_files $uri =404;
fastcgi_pass unix:/opt/local/var/run/php54/php-fpm-images.sock;
include /documents/projects/intahwebz/intahwebz/conf/fastcgi.conf;
}
The location block doesn't need to know about the URL rewrites because they are done before the location block is encountered.

Apache rewrite rule similar to Nginx try_files

In Nginx I played around with try_files which basically took any request for a file on the domain and passed it through a custom php script called file_parse.php. In Nginx it looked like this: try_files $url /file_parse.php
If the file did exist in the document root then it did not use try_files. This rule in Nginx doesn't redirect the user, for example if a user types in http://www.domain.com/123456.html that address shows in their browser but file_parse.php takes 123456.html and echo's out html code based on the number (123456). If file_parse.php doesn't have anything to echo out then file_parse.php sets a 404 header for the client.
Does something like this exist in Apache?
Found an answer, seems to work, no errors in error.log:
<Directory /this/is/the/directory/>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . file_parse.php [L]
</Directory>
Does something like this exist in Apache?
Take a look at mod_rewrite's RewriteMap directive, in particular the prg map type which allows you to run a script and pass it request info.