1

您好,我正在尝试将这些重写为 nginx 格式,但无法取得任何进展。你能帮我为 nginx 重写这些吗?

# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|\?)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /render to the image parser
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]

我已经尝试过了,但它没有用。

location  public/ {
}

location ~ (\.|\?) {
}

location ~ (.*)/$ {
}

location  public/ {
}

location / {
  if (!-e $request_filename){
    rewrite (.+)\.(html|json|xml|atom|rss|rdf|txt)$ /$1/ break;
  }
  if (!-e $request_filename){
    rewrite ([^/]+)$ /$1/ break;
  }
   if ($request_uri ~ "render/"){
    rewrite ^/render/. /app/parsers/slir/ break;
  }
  if ($request_uri ~ "/app/$"){
    rewrite ^/app/ /index.php break;
  }
   if (!-e $request_filename){
    rewrite ^/(.*)/$ /index.php?/$1/ break;
  }
  if (!-e $request_filename){
     rewrite ^(.+)$ /public/$1 break;
  }
}

当我尝试这个并输入我的网址时。它下载一个文件。

4

1 回答 1

0

我不确定它是否完全符合您的问题,但这应该会给您一些帮助和下一步的起点。还有一个建议 - nginx 的工作方式与 Apache 有点不同,所以不要尝试使用 nginx 之类的东西if (!-e $request_filename)- 这是一个非常糟糕的主意(关于这里的东西:不要那样做

# It's your first rule:
# If request location match this regex then
# try file as request, if not exist
# try a folder matching * or
# return with 404
location ~ (.+)\.(html|json|xml|atom|rss|rdf|txt)$ {
   try_files $uri $1/ =404;
}

# It's firts part of your second rule:
# If request match this regex 
# try file as request if not exist
# try file with added slash or
# return with 404
location ~ !(\.|\?) {
   try_files $uri $uri/ =404
}

# It's second part of your second rule:
# If request match this regex
# try file as request if not exist
# try file with added slash or
# return with 404
location ~ !(.*)/$ {
   try_files $uri $uri/ =404
}

# It your third rule:
# Rewrite /render to /app/parsers/slir
location = /render {
   rewrite .* /app/parsers/slir break;
}

# It's first part of your forth rule:
# If request location match this request then use /index.php
# or return with 404
location ~ ^/app/$ {
   try_files /index.php =404;
}

# It's second part of your forth rule:
# If request location match regex try to use it,
# if not try /index.php?/$1/ location
# or return with 404
location ~ ^(.*)/$ {
   try_files $uri /index.php?/$1/ =404;
}

# It's your fifth rule:
# Try file with match this reqex, if not try location /public/$1
# or return with 404
location ~ ^/(.+)$ {
   try_files $uri /public/$1 =404;
}

# It's a global rule
# For all request try file as it is
# or return with 404
location / {
   try_files $uri =404;
}
于 2013-01-26T15:59:57.363 回答