Welcome! Log In Create A New Profile

Advanced

Help with rewrite (to nginx server blocks)

Posted by morespinach 
Help with rewrite (to nginx server blocks)
August 06, 2014 11:15AM
Conditions:

- I don't want to use IF conditions in Nginx conf.
- Hoping to try this via `try_files` alone, including a pattern match?
- The PHP code on my site does not have any `.php` extensions (Apache currently has a default handler of PHP, so even without extensions, it's PHP that's executed)

My rewrite rules:

<code>
RewriteEngine On

# --------| MAIN DOMAIN (without any file) should go to the beta folder
RewriteRule ^/$ /beta/index [L]
RewriteRule ^$ /beta/index [L]

# --------| IF SUBDOMAIN, SEND TO /beta/score with "type=[sport]"
RewriteCond %{HTTP_HOST} ^(baseball|soccer)\.domain1|domain2)\.(com|net)
RewriteRule ^/(.+)$ /beta/score?u=$1&type=%1 [L]

# --------| ANYTHING INSIDE BETA FOLDER, SERVE FROM there
RewriteRule ^/beta/(.*)$ /beta/$1 [L]

# --------| EVERYTHING ELSE (OUTSIDE BETA FOLDER) GOES TO SCORE PROGRAM
RewriteRule ^/([-~a-zA-Z0-9_\.]+)$ /beta/score?u=$1 [L,QSA]
</code>


SO, just to explain the rules:

http://domain1.com -> http://domain1.com/beta/index
http://domain2.com -> http://domain1.com/beta/index
http://domain1.com/ -> http://domain1.com/beta/index

http://baseball.domain1.com/chicago -> http://domain1.com/beta/score?u=chicago&type=baseball
http://baseball.domain1.com/boston -> http://domain1.com/beta/score?u=boston&type=baseball
http://baseball.domain1.com/boston/ -> http://domain1.com/beta/score?u=boston&type=baseball
http://soccer.domain1.com/boston/ -> http://domain1.com/beta/score?u=boston&type=soccer


http://domain1.com/beta/abc -> http://domain1.com/beta/abc
http://domain1.com/beta/xyz -> http://domain1.com/beta/xyz

http://domain1.com/abc -> http://domain1.com/beta/score?u=abc
http://domain1.com/xyz -> http://domain1.com/beta/score?u=xyz
http://domain1.com/xyz<1*&^ -> http://domain1.com/beta/score?u=xyz
http://domain1.com/xyz,,,,1 -> http://domain1.com/beta/score?u=xyz


I need the last bit to make sure we don't get drowned in 404s. Which has happened.

How would I write the above into "location" blocks? I could do that last rule as such:


APACHE--
RewriteRule ^/([-~a-zA-Z0-9_\.]+)$ /beta/score?u=$1 [L,QSA]

NGINX--
rewrite ^/([-~a-zA-Z0-9_\.]+)$ /beta/score?u=$1 last;



But where does this go? I'm thinking a main "`/`" location block is needed, inside which will need to be the usual GIF and JPG declarations..and then a block for /beta/ folder....then a check for the last rewrite rule...it's mind-numbing where things go.

Right now, my nginx has this, which basically shovels all the PHP work to Apache:

location / {

location ~.*\.(gif|jpg|png|ico|swf|rss|xml|htm|txt|js|css|gz|doc|xls|pdf|html|woff|eot|svg)$ {
expires max;
try_files $uri @backend;
log_not_found off;
}

proxy_pass http://100.99.98.97:5000;
include proxy.inc;
}


location @backend {
internal;
proxy_pass http://100.99.98.97:5000;
include proxy.inc;
}



Many thanks for any ideas!
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 93
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready