Score:0

Nginx redirect rule to sub-directory

in flag

I have an old PHP application running with Apache, for now I need to switch to use Nginx

htaccess redirecting rule

php_value upload_max_filesize "5M"

AddType text/x-component .htc


RewriteEngine On
RewriteBase /brand/kit/summer-icecream/lacne

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(/admin/)
RewriteCond %{REQUEST_URI} !(/setup/)
RewriteCond %{REQUEST_URI} !(/support/)
RewriteCond %{REQUEST_URI} !(/share/)
RewriteCond %{REQUEST_URI} !(/rss/)
RewriteCond %{REQUEST_URI} !(/output/)
RewriteCond %{REQUEST_URI} !(/upload/)
RewriteCond %{REQUEST_URI} !(/app_api/)
RewriteRule ^(.+)\.php$ share/admin/$1.php [L]

RewriteRule app_api/(.+)\.php$ share/app_api/$1.php [L]

I have tried several way to redirect such as below but it seems doesn't work at all, it doesn't pass the PHP file to PHP-FPM

location ~ brand/kit/summer-icecream/lacne/(.+)\.php$ {
        if (!-e $request_filename){
           rewrite ^/brand/kit/summer-icecream/lacne/(.+)\.php$ /brand/kit/summer-icecream/lacne/share/admin/$1.php redirect;
        }
    }

    location ~ (.+)\.php$ {
       try_files $uri =404;
       fastcgi_pass   fastcgi_backend;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
       fastcgi_read_timeout 600s;
       fastcgi_connect_timeout 600s;
       fastcgi_param   REMOTE_ADDR $remote_addr;
       fastcgi_param HTTP_HOST $host;
       include /etc/nginx/fastcgi_config;
    }

For testing, the request URI is: http://127.0.0.1:8989/brand/kit/summer-icecream/lacne/login.php

djdomi avatar
za flag
Well you can run nginx and apache parallel because its a common setup - let the old thing run on apache and use nginx to proxy_pass it, isnt that an option?
user2306811 avatar
in flag
well that way will make the stack more complex for further maintenance, if there is no option with Nginx, I have to come back with Apache
Gerard H. Pille avatar
in flag
Perhaps nginx isn't listening on 127.0.0.1:8989. Hard to say with only an extract from your configuration.
Ivan Shatsky avatar
gr flag
Using `if (!-e $request_filename) { ... }` is a bad practice (usually this construction suggested by online apache-to-nginx configuration converters). You can try [this](https://www.getpagespeed.com/apache-to-nginx) converter (made by [Danila Vershinin](https://serverfault.com/users/242885/danila-vershinin)).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.