Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Apache .htaccess file to be used with Nginx

I'm migrating a website from a server that has Apache web-server to another server that is running Nginx web-server, and I wanted to convert the .htaccess files, the problem is not just the syntax but also the file name, is it also ".htaccess" or what?

like image 207
Amir Iskander Avatar asked Jan 03 '12 11:01

Amir Iskander


People also ask

Can I use htaccess with nginx?

Nginx does not use . htaccess files like Apache does. This means that configuration previously done in . htaccess files now has to be done in a different format, explained in the Nginx documentation.

How do I enable htaccess in nginx?

import your . htaccess to nginx. conf (maybe the htaccess to nginx converter helps you) use authd-htpasswd (I didn't try it)

What is the nginx equivalent to htaccess?

Since Nginx does not have an equivalent to the . htaccess file (i.e. no directory level configuration files), you need to update the main configuration and reload nginx for any changes to take effect.


2 Answers

Here's a tool I use:

http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

It is not 100% accurate but it's pretty good base

Also, here's a link about converting the rules:

http://nginx.org/en/docs/http/converting_rewrite_rules.html

This one can help a little:

http://wiki.nginx.org/HttpRewriteModule#rewrite

EDIT:

The file name should be nginx.conf

like image 109
Book Of Zeus Avatar answered Sep 22 '22 13:09

Book Of Zeus


Nginx does not have support for .htaccess files.

But .htaccess files are bad. It's a way to put parts of the apache configuration erverywhere on the filesystem and to tell apache to check the filesystem at every request to see if you do not have some special configuration in place. The only real usage of .htaccess file is to delegate a small part of the configuration to the user if you are a host provider and wanted to allow some web server configuration for your users.

Nginx configuration is compiled when nginx restart. Of course you can include several files that you can put in your web directory if you really do not care about information disclosure problems (like users seing your web server configuration), but having the same thing as a .htaccess, read each time a request is done, is not in the nginx way.

http://wiki.nginx.org/LikeApache-htaccess:

You can't do this. You shouldn't. If you need .htaccess, you're probably doing it wrong.

like image 42
regilero Avatar answered Sep 25 '22 13:09

regilero