1

It's about 5 days I'm trying to launch my website without any success. The problem is I use apache on the local and my website works well in local, and my server uses nginx and I cannot use .htaccess conversion inside nginx configuration.

Here is a simplified of my website structure:

/mywebsite
    /application
    /files
        file1.php
        .htaccess
    /public
        /css
        /js
.htaccess

See? I have two .htaccess files. One is located on the root and another one is inside files directory. It all works on the localhost, since I use apache on localhost. Now I need to make it working on the server which uses nginx.

I use this website to convert the content of htaccess files to nginx-configuration.

First of all, should I paste the result of conversion in what file? (where is nginx configuration file located? /etc/nginx/nginx.conf ?)

And how can I handle those two .htaccess files? Should I make two nginx files too?


.htaccess file in the root:

RewriteEngine on
Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

ErrorDocument 404 /error404.html

Options -Indexes

<Files *.php>
    Order Deny,Allow
    Deny from all
    Allow from ::1
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

.htaccess file which is inside files directory:

<Files *.php>
   Allow from all
</Files>
0

In nginx, all site-specific configurations are included in one server block, and location blocks are used to add different configuration directives for particular URLs.

Overall, the philosophy is quite different from Apache2, so you need to study it in order to understand how to make similar configuration with it.

In your case, nginx configuration directives might look something like this:

location / {
    try_files $uri $uri/ /index.php?rt=$request_uri;
}

location ~ \.php$ {
    deny all;
}

location ^~ /index.php {
    # include here the configuration items from nginx default location ~  \.php$ block
}

location ^~ /files {
    # include here either PHP configuration directives from location ~ \.php$ block if you want PHP scripts executed from here. If you do not want PHP scripts to be executed, then use
    allow all;
}

These directives are included either in main nginx configuration, or site-specific configuration which exist under /etc/nginx/sites-available directory.

Some explanations on the blocks:

The first location block is the standard front controller pattern implementation on nginx. It means nginx first checks if the required file is found somewhere on server, servers it if one exists. Otherwise it sends the request to index.php, with the original request URI part as an argument to ?rt. This is slightly different than your implementation, since there you use a regex to limit the possible URIs passed as argument.

The second location block rejects access to all URIs ending with .php$.

The third block adds an exception for index.php, which is processed using PHP backend.

The fourth block either sends PHP script requests to PHP backend, or simply allows sending them to the user.

As a disclaimer, I cannot test these rules since I don't know your software environment, so these might not fulfill your requirements, or they might fail for some parts.

  • If you are unfamiliar with Nginx then I think this configuration might give you some unintended results that you find difficult to understand. The third block uses prefix matching on a suffix, so will only apply to example.com/index.php, but not example.com/anydir/index.php I would change the second block to include an exception for index.php, either with a rewrite directive or modified regex. You also need a root directive and you need to pass your php handling to an upstream instance of php fpm, – miknik Jul 11 '18 at 1:23
  • As far as I understand from the original Apache rules, the intention is to deny access to all .php files except for /index.php, so that means anydir/index.php should be blocked, which happens with these rules. – Tero Kilkanen Jul 11 '18 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.