Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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:

/var/www/mydomain.tld/

└── 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 {
  listen 1.2.3.4:80;
  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.pl index.xhtml;

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

 **** 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_pass 127.0.0.1:9010;
  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

1 Answer 1

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: 1.2.3.4, server: my_domain.tld, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9010", 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

 
discard

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.