1

I want to migrate a PHP application from Apache to Nginx. The problem is that the App breaks, because the routing doesn't work anymore and I'm not exactly sure how to fix it.

The PHP application includes some .htaccess files and I tried to convert those to Nginx.

The first one is in the document root:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule    ^$    public/    [L]
  RewriteRule    (.*) public/$1    [L]
</IfModule>

The second one is in /public/

<IfModule mod_rewrite.c
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  # Rewrite all other URLs to index.php/URL
  RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>

The third and last one is:

deny from all

My nginx version of it looks like the following:

#user nobody;
worker_processes 1;


#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    #tcp_nopush on;
    keepalive_timeout 65;
    gzip on;

    server {
      listen 8080;
      server_name localhost;
      root /Library/WebServer/Documents/admin;

      location / {
        index index.php;
        rewrite ^/$ /public/ break;
        rewrite ^(.*)$ /public/$1 break;
      }

      location /public {
        if (!-e $request_filename){
          rewrite ^(.*)$ /index.php?url=$1 break;
        }
      }

      location /library {
        deny all;
      }
      #error_page 404 /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root html;
      }

      location ~ \.php$ {
        root /Library/WebServer/Documents/admin;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
       }
   }
}

The problem I face is that something that the routing is broken and just returns a 404 page instead. Hopefully someone has an idea and know how to fix it ;)

Thanks

EDIT

I got it working with this config

location /library {
     deny all;
   }

   location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
       access_log off;
        rewrite ^(.*)$ /public/$1 break;
      }

      location / {
       rewrite ^/(.+)$ /index.php?url=$1 last;
      }

I'm sure there are better solutions and I'm open for suggestions.

3
  • You say the issue is with the routing, can you confirm that you are getting nginx/PHP to work outside of this application? Nov 7, 2013 at 6:27
  • Yes, I can confirm that it's working. I also run wordpress locally with a similar setup. I think it must be the rewrite rule, which breaks it somehow, but I don't know how to fix it.
    – user196605
    Nov 7, 2013 at 6:31
  • If I go to /index.php?login it works for Example. But when the user types /login it should rewrite it to /index.php?login.
    – user196605
    Nov 7, 2013 at 7:09

1 Answer 1

1

It appears as though you have a routing loop.

You are directing all traffic from / to /public and then all traffic from /public to /index.php

Based on /public/.htaccess, all requests to /public should be routed to /public/index.php?url=$1 instead of /index.php?url=$1

 location /public {
        if (!-e $request_filename){ 
          rewrite ^(.*)$ /public/index.php?url=$1 break;
        }
      }
3
  • Hey David, I tried that but it didn't work. I get a 404 when I go for example to /users. I updated the config above and this seems to work.
    – user196605
    Nov 7, 2013 at 9:14
  • /public/index.php?url=$1 and /index.php?url=$1 do the same.
    – user196605
    Nov 7, 2013 at 9:15
  • Glad you got it working :) Nov 7, 2013 at 9:22

You must log in to answer this question.