0

with apache I use a file config.php to rewrite a file config.js based on domain, config.js contains placeholders which are replaced by RewriteRule directive, this is .htaccess file:

<IfModule mod_rewrite.c>
    RewriteBase /foo
    RewriteEngine On
    RewriteRule config.js config.php [L,QSA]
</IfModule>

When i go into URL production.com/foo/config.js i see some values, instead into develop.com/foo/config.js i see other values, defined into config.php

Now i need move to nginx but i don't understand how to replicate the apache rule, i tried:

location /foo {
  alias /src/www/foo;
  index index.html;
  rewrite config.js config.php break;
  }

But have internal server error

Thanks

1
  • Sorry, I write about last instead of break but didn't change it in a config code block. Updated an answer. Commented Oct 7, 2020 at 13:12

1 Answer 1

2

All nginx URIs starts with slash. Using rewrite config.js config.php you are rewriting an URI from /foo/config.js to config.php. That URI cannot be processed by nginx causing internal server error.

First argument of rewrite directive is always treated as a regex where dot matched any symbol. So your rewrite rule would match any string containing config*js substring. I don't think it is what you really want.

Second argument of rewrite directive is a whole new URI, not a substitution part of the string. To make a substitution use something like rewrite ^(.*)old-string(.*)$ $1new-string$2.

Since you are rewriting your request to PHP script you should force nginx to process new URI with a location where your PHP-FPM handler is defined. To do it you should use last flag instead of break one with the rewrite directive.

Summing all of this, you need something like

location /foo {
    alias /src/www/foo;
    index index.html;
    rewrite ^/foo/config\.js$ /foo/config.php last;
}

One more note. As nginx documentation states:

When location matches the last part of the directive’s value:

location /images/ {
    alias /data/w3/images/;
}

it is better to use the root directive instead:

location /images/ {
    root /data/w3;
}

so it would be better to use root /src/www; instead of alias /src/www/foo; within this location block if that foo substrings are really equal.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for reply, in this way when i go to example,com/foo/config.js i see the file config.php
@hellb0y77 So you do not set up nginx to work with PHP-FPM (or any other PHP handler) yet? You should have php-fpm installed on your server and a special location ~ \.php$ { ... } defined to use it (this is the most common approach, there are other choices, NGINX Unit for example). Default nginx config should have an example for using PHP-FPM (commented by default). This is out of the scope of your question, check this one.
nginx work with php-fpm, I've tried to place a phpinfo file at the same path of config.php and work.
@hellb0y77 Do you have rewrite ... last;, not the rewrite ... break;? Maybe your browser cached the response?
yes rewrite ^/foo/config.js$ /foo/config.php last;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.