Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to migrate an application (or its .htaccess-files) written for Apache to Nginx and I've come across a problem I just can't wrap my head around. I'm from an Apach environment, so this is quite new for me.

I have the following folder structure:


└── web
    ├── error
    ├── favicon.ico
    ├── my_application
    ├── index.html
    └── robots.txt

web is the www-root-folder of mydomain.tld, my application is in /web/my_aplication/. I want to serve the application in /my_application when www.my_domain.tld is called.

The standard directives are created automatically by ISPConfig, but I guess I need to add a few lines, but I'm not sure what I'm doing wrong. Here are the relevant parts (I guess):

server {
  server_name my_domain.tld;
  root   /var/www/mydomain.tld/web;

  ## force www - added this myself
  if ($http_host !~ "^www\.") { 
    rewrite ^(.*)$ http://www.$http_host$request_uri redirect; } 

  ## rewrite to /my_application/ and last
  if ($http_host ~* "^www.my_domain.tld$") { 
    rewrite ^/(.+)$ /my_application/$1 last; }

  index index.php index.html index.htm index.cgi index.xhtml;

  error_page 400 /error/400.html;
  # ....
  recursive_error_pages on;
  location = /error/400.html {
  ## ....

 **** this part here is explained below ****


Except for the force-www-part, these directives were generated automatically and should actually work fine.

But now it comes to translating the .htaccess-rules from within the my_application folder. I used the htaccess to nginx converter and got some Nginx-directives wrapped in location / { ... }. I changed the location to /my_application and put the directives in the place mentioned above:

locaction /my_application {
  rewrite (.*/)?info/([A-Za-z0-9_-]+)\.html.* /shop_content.php?gm_boosted_content=$2&$query_string break; 
  rewrite (.*/)?([A-Za-z0-9_-]+)\.html /product_info.php?gm_boosted_product=$2&$query_string break; 
  rewrite (.*/)?([A-Za-z0-9_-]+)/?.* /index.php?gm_boosted_category=$2&$query_string break;

## These are automatically generated directives for the root-folder (web, I guess).
## Please note that I have placed my own directives above them, I don't know
## if that mattes though.
location ~ \.php$ {
  try_files $uri =404;
  include /etc/nginx/fastcgi_params;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_script_name;
  fastcgi_intercept_errors on;

So, actually nothing too fancy, but I just can't get it working... What happens instead: The index.php is being downloaded (not executed). But I don't quite know why, because this approach is quite the same as what I've found via Google. But I'm probably missing something.

share|improve this question

You are using break flag on rewrite instead of last. Break means that Nginx should rewrite the internal URI but not re-evaluate location directives, thus it's never passed to PHP and sent for download instead.

Also the reason it's being downloaded and not shown is that you have no mime type added for php files and your default_type is configured to application/octet-stream.

share|improve this answer
Thank you for having a look at it - the Nginx configuration is actually ok (it was done automatically), the MIME-types are set in another file. I've changed the break-flags to last, but now I get the following error: rewrite or internal redirection cycle while internally redirecting to "/error/404.html" while sending to client, client:, server: my_domain.tld, request: "GET / HTTP/1.1", upstream: "fastcgi://", host: "www.my_domain.tld" I've seen this before while I was playing around with the directives, but I'm not quite sure what it means or how to fix it. – Quasdunk Oct 26 '12 at 20:12
Could it be that the server is looking for /error/404.html in /web/my_application instead of in /web (where it actually is suppoed to look)? – Quasdunk Oct 26 '12 at 20:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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