1

I'm a newbie to nginx. I have the nginx before Apache and I'm having problems.

I seem to need 2 rules in the config:

Rule 1) if we have url "/my_path" we don't touch it but user goes to "/my_path.php"

Rule 2) if we have url "/my_path.php" we change it to "/my_path" and user goes to "/my_path.php" BECAUSE we revert to rule 1.

I think this is necessary for SEO?

My configuration, for example is this...

server {
    listen 8080;
    server_name my_apache_server;

    location / {
        proxy_pass http://my_apache_server/;
    }

    location ~^\/(.+)$ {
        rewrite ^\/(.+)$ /$1.php break;
        proxy_pass http://my_apache_server/;
    }

    location ~^\/(.+)\.php$ {
        proxy_redirect http://my_apache_server/$1 http://my_apache_server/error.php;
    }

    ...
}

But nginx shows an error:

nginx: [emerg] "proxy_pass" cannot have URI part in location given 
by regular expression, or inside named location, or inside "if" statement, 
or inside "limit_except" block

As I understand it, the proxy_pass can't be used with regular expressions. Is this correct? I am thinking my config is wrong?

Can anyone help? Thanks for your help in advance.

MY SOLUTION - it works

    # index page
    location / {
        proxy_pass http://my_apache_server;
    }

    # redirect(301) real "name.php" file to "name"
    location ~ ^/([A-Za-z_]+).php$ {
        rewrite  ^/([A-Za-z_]+).php$  /$1  permanent;
    }

    # not existen a-z file name to name.php
    location ~ ^/([A-Za-z_]+)$ {
        proxy_pass http://my_apache_server/$1.php;
    }
2

Can't proxypass to a URI, this would make for a double slash after the URL (http://my_apache_server//$.php).

Try:

location ~^\/(.+)$ {
  rewrite ^\/(.+)$ /$1.php break;
  proxy_pass http://my_apache_server;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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