33

There are many tutorials on how to write Nginx rewrite rules to mimic Apache's .htaccess file but I can't confidently say I know what the name or location of this so-called Nginx config file actually is. I'm specifically looking for the Nginx config file that allows you to write server path and PHP directives. I'm using Ubuntu 12 linux and Codeigniter as my PHP framework.

Here's what I know so far:

  1. This file: /etc/nginx/nginx.conf despite its enticing name is apparently not the place for server directives.
  2. This file: /etc/nginx/sites-available/default seems like what I need (i'm able to generate php errors specific to my app) but I can't get the Codeigniter-specific path routing correct. First of all, is this the "config" file/.htaccess equivalent everyone speaks of?
tim peterson
  • 683
  • 2
  • 9
  • 18
  • Your problem isn't with PHP **the language** (i.e programming in PHP), it's with PHP **configuration** on a specific server, which explains the completely valid close votes. I cannot comment on the latter part of your post, as it seems highly subjective and biased. – nickb Jul 31 '12 at 03:17
  • 1
    You can find where nginx config is located using `nginx -V` which you can find in the manual with `man nginx`. The `-V` option is documented as "-V Print the nginx version, compiler version, and configure script parameters.". If `nginx` is not in your path, use `which nginx` which returns the full path to nginx `$(which nginx) -V` – bucabay Apr 16 '16 at 20:31

1 Answers1

29

I use PHP with nginx on my development server with the following configuration:

  1. in /etc/nginx/nginx.conf I added: include /etc/nginx/sites-enabled/*; before the last }.

  2. run the following commands to create your site configurations:

    mkdir /etc/nginx/sites-available/
    mkdir /etc/nginx/sites-enabled/
    cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/<site>
    
  3. edit the site configuration, some possible configurations (this is what I referred to in my setup): http://kbeezie.com/view/nginx-configuration-examples/

  4. link to the available site to enable it

    ln -s /etc/nginx/sites-available/<site> /etc/nginx/sites-enabled/<site>
    
  5. keep in mind that you can actually include files within the configurations almost anywhere, this makes creating multiple sites easy when you want to throw similar configurations together.

Yes, .htaccess is compareable to the server{} block in nginx, but they are also very different. nginx has a much more lightweight approach to parsing configuration, and it will not scan site directories for additional configuration, only whatever is seen by nginx.conf, goes.

abestic9
  • 399
  • 2
  • 3
  • -@Andrew thanks lots of helpful information!, by default my `/etc/nginx/nginx.conf` file already has `include /etc/nginx/sites-enabled/*;` at the very end. It looks like these 2 files are talking to each other on my server and now I'm dealing with Codeigniter-specific issues. Are you using codeigniter? There is some path funkiness that I need to get a handle on... – tim peterson Jul 31 '12 at 02:36
  • I use my own framework which is quite similar (albeit more primitive) to CodeIgniter. At the moment I am trying to get my mod_rewrite regexp rules converted to nginx's HttpRewrite module. My first major breakthrough was getting past the 403/404 errors nginx displays when I have incorrectly configured the `root` parameter. Feel free to check out my Questions if you need any inspiration. – abestic9 Jul 31 '12 at 02:41
  • thanks for the tip on checking out your questions. I'm thinking i'm understanding more now, thanks for the setting me on the path... – tim peterson Jul 31 '12 at 02:43