简体   繁体   English

如何自动执行此nginx重写规则?

[英]How can I automate this nginx rewrite rule?

I'm afraid I'm in way over my head. 我担心我会超过头脑。 I'm sure I'm missing something simple, but it's not clicking together so far. 我确定我错过了一些简单的东西,但到目前为止它还没有点击。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I've got a domain that hosts a number of micro sites, structured like so: 我有一个域名,其中包含许多微型网站,结构如下:

  • domain.com/site1 domain.com/site1
  • domain.com/site2 domain.com/site2
  • domain.com/site3 domain.com/site3
  • ... ...

I'm converting over from Apache to Nginx. 我正在从Apache转换到Nginx。 When I was using Apache, this is the set of rewrite rules that worked for as many folders as I wanted. 当我使用Apache时,这是一组重写规则,适用于我想要的多个文件夹。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.*)$ %2index.php [QSA,L]

At this point, I am able to get the sites working using individual rules. 此时,我可以使用个别规则使网站正常工作。 But it's not really a feasible solution for my circumstance. 但对我的情况来说,这不是一个真正可行的解决方案。

location /site1 {
    try_files $uri $uri/ /site1/index.php?$args;
}
location /site2 {
    try_files $uri $uri/ /site2/index.php?$args;
}
location /site3 {
    try_files $uri $uri/ /site3/index.php?$args;
}

There's gotta be a better way. 必须有一个更好的方法。 I'm just such a noobie with Nginx, I can't quite figure it out. 我只是和Nginx一样,我无法弄明白。 Thanks in advance for your help. 在此先感谢您的帮助。

After reading a reply on my first answer I've changed it to this, don't know if you'll need the $suburi but it's there just in case. 在我第一个回答的回复后,我已将其更改为此,不知道您是否需要$suburi但是为了以防万一。

server {
  server_name example.com;
  index index.php; # need this
  location / {
    try_files $uri $uri/ /index.php;
  }
  location ^~ (?<subfolder>/[^/]+)(?<suburi>.+) {
    try_files $uri $uri/ $subfolder/index.php$is_args$query_string;
  }
}

Alternative with no named capture blocks 没有命名捕获块的替代方案

  location ^~ (/[^/]+)(.+) {
    try_files $uri $uri/ $1/index.php$is_args$query_string;
  }

I finally found a solution here: https://github.com/rtCamp/easyengine/issues/31 我终于找到了一个解决方案: https//github.com/rtCamp/easyengine/issues/31

set $dir "";
if ($request_uri ~ ^/([^/]*)/.*$ ) {
  set $dir1 /$1;
}
location / {
  try_files $uri $uri/  $dir1/index.php?$args;
}

I think about something like these: 我想到这样的事情:

location ~ ^/(.*)/*.*$ {
    try_files $uri $uri/ /$1/index.php?$args;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM