52

I am currently migrating my website from Apache to nginx, but my .htaccess file is not working. My website is inside the /usr/share/nginx/html/mywebsite folder. How can I use .htaccess in my nginx server?

This is my .htaccess file:

RewriteEngine on
RewriteRule video/watch/([a-zA-Z0-9_@$*-]+)/?$ "videos-single.php?id=$1" [NC]
apaderno
  • 28,547
  • 16
  • 75
  • 90
Sandeep Bhaskaran
  • 621
  • 1
  • 7
  • 14

5 Answers5

61

Nginx doesn't support .htaccess (see here: "You can’t do this. You shouldn’t. If you need .htaccess, you’re probably doing it wrong.").

You've two choices (as I know):

danronmoon
  • 3,814
  • 5
  • 34
  • 56
uzsolt
  • 5,832
  • 2
  • 20
  • 32
  • I think `/etc/nginx/nginx.conf` - see the `nginx` package's filelist. – uzsolt Mar 05 '16 at 10:02
  • 4
    What if you are using a shared server? You don't have access to /etc/nginx then is there a config file similar to .htaccess for the folder where your subdomain is? – NaturalBornCamper Jan 07 '18 at 16:12
  • 1
    @NaturalBornCamper Then I guess you will have to use Apache. – Uri Jul 16 '19 at 09:53
  • 1
    @NaturalBornCamper I think you will have your own config file for your site, you either contact your hosting provider or if you manage it you specify you configuration for your site's domain only – moghwan Jan 05 '23 at 11:17
23

Disclosure: I am the author of htaccess for nginx, which is now open source software.

Over the past years, I created a plugin which implements htaccess behaviour into nginx, especially things like RewriteRule, Allow and Deny, which can be crucial for web security. The plugin is used in my own productive environments without a problem.

I totally share the point of efficiency and speed in nginx, and why they didn't implement htaccess. However, think about it. You cannot make it worse if you're using nginx plus htaccess. You still keep the great performance of nginx, plus you can drive your legacy appliances effortlessly on one webserver.

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Gerald
  • 955
  • 1
  • 8
  • 18
  • 1
    Too hard to integrate your software. You should write a full tutorial – Tony Nov 04 '20 at 16:19
  • Can you check this error: "failed to load external Lua file "/etc/nginx/htaccess-for-nginx/htaccess-1.2.1-bytecode.lua": /etc/nginx/htaccess-for-nginx/htaccess-1.2.1-bytecode.lua: cannot load incompatible bytecode". It caused Nginx to response 500 error – Tony Nov 04 '20 at 16:39
  • @Tony looks like a Lua version mismatch. Make sure to install the right Lua JIT version. – Gerald Jan 11 '21 at 10:05
  • 3
    I decided to release my code as an open source project: https://github.com/e404/htaccess-for-nginx Pull requests are very welcome. – Gerald Jan 25 '21 at 20:04
  • ...Open Source, but you need a license if you want to use it in any way that makes sense. – Sebi2020 Apr 17 '21 at 10:20
  • @Sebi2020 no license required. Did you use the current version from here? https://github.com/e404/htaccess-for-nginx – Gerald May 03 '21 at 13:21
  • @Gerald Your homepage states something different. Quote: "Test this plugin now, limited to 100 requests per server (re)start, without any subscription." Source: https://htaccess-for-nginx.com/ Or do we talk about different products? – Sebi2020 May 04 '21 at 11:03
  • @Sebi2020 I see a very classic MIT licence inside the github repo and the buttons in front of the text you are referring to both lead (download and commercial licence) to a 404 page. So I would definitely use it without any doubt if I needed to. – Zeitounator May 07 '21 at 22:47
  • @Zeitounator cannot confirm that the buttons lead to 404 pages. That's interesting. I get a zip archive if I click download. – Sebi2020 May 08 '21 at 13:16
  • @Sebi2020 might have been fixed overnight then (either the buttons or the keyboard/chair interface on my side ;)) Meanwhile, [I still see a MIT license on the github repo](https://github.com/e404/htaccess-for-nginx/blob/main/LICENSE). And if you look closely, this is at the end a single LUA script. So once again: I would use it if needed from github without asking myself any other questions. This would not be the first dual license commercial/freeOSS product on the market. – Zeitounator May 09 '21 at 08:25
  • 2
    Off topic, but it's a pet peeve. It's a "disclosure" not a "disclaimer". You are disclosing that you are the author. – liamvictor Sep 30 '21 at 11:11
  • 2
    @liamvictor See these questions on [EL&U](https://english.stackexchange.com/q/115850/239338) and [ELL](https://ell.stackexchange.com/q/278312/51868). – iBug Jun 28 '22 at 12:08
10

This is not supported officially in nginx. If you need this kind of functionality you will need to use Apache or some other http server which supports it.

That said, the official nginx reasoning is flawed because it conflates what users want to do with the way it is done. For example, nginx could easily check the directories only every 10 seconds / minute or so, or it could use inotify and similar mechanisms. This would avoid the need to check it on every request... But knowing that doesn't help you. :)

You could get around this limitation by writing a script that would wait for nginx config files to appear and then copy them to /etc/nginx/conf.d/. However there might be some security implications - as there is no native support for .htaccess in nginx, there is also no support for limiting allowed configuration directives in config files. YMMV.

johndodo
  • 17,247
  • 15
  • 96
  • 113
  • 1
    I don't disagree with the reasoning that scanning for files is useless usage of resources, but it definitely doesn't need to be that extreme. It could be made to look only at the root directory for an extra conf file defined by the end-user. It would need to become its own new conf block (DirectoryOverride?) and reload nginx confs in memory by looking at file modification time. 1) It wouldn't even need to execute at every request; 2) It would allow pre-validating and caching of the end-user conf; 3) It would still allow end-user to use location blocks to protect directories and add rewrites. – davidwebca Nov 29 '22 at 22:39
2

Using the config file is one option, but the cool thing about the .htaccess file is that it provided a way for a web developer to have some control over server settings without having root access to the server. There doesn't seem to be anything like this on nginx which is a real bummer.

I understand how the way it's setup on apache slows down response times, but hoped there could be an nginx way to do the same thing without the performance hit... At least a way to do rewrites with regex on urls if nothing else.

Condensed
  • 21
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 28 '22 at 19:15
-1

"Is there no nginx way to do bulk redirects using regular expressions that doesn't slow down response times."


Just edit your database with myphpmyadmin.

  1. Open myphpmyadmin select your database then find your "yourprefix_Posts" table.
  2. Open it then click the "Search" tab, then "Find and Replace".
  3. Select "post_content" in the dropdown
  4. In the "Find" field, type URL you want to change: "website.com/oldURL".
  5. In the "Replace" field, type the new URL: "website.com/newURL". (To use regular expression, tick the "Regular Expression" box.)

NOTE: You can test this out by simply leaving the "Replace" field blank.

ALWAYS BACKUP database before making changes. This might sound scary but its really not. Its super simple and can be used to quickly replace just about anbything.