【问题标题】:Convert .htaccess to nginx, seeking working solution将 .htaccess 转换为 nginx,寻求工作解决方案
【发布时间】:2015-08-25 01:59:27
【问题描述】:

您好,我知道外面有一些 .htaccess2nginx 转换器,好吧,仍然需要一些帮助,因为它根本不工作。

我的问题: 在 Nginx 上设置 Imageboard(MyImouto), Imageboard 使用 .htaccess 从 /srv/domain/public/ 重定向到 /srv/domain/app/...

这是.htaccess:

RewriteEngine On

# Images redirection.
# The following RewriteCond will try to deny any direct linking to images.
# RewriteCond %{HTTP:Referer} ^http://domain/
RewriteRule ^(?:data/)?(preview|sample|jpeg|image)/([0-9a-f]{2})([0-9a-f]{2})([$

# Serve gzipped Stylesheets and Javascripts if mod_headers are enabled.
<IfModule mod_headers.c>
    SetEnv no-gzip
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(assets/.*)\.css $1\.css\.gz [L,ENV=ASSETSCSS:true] [L]
    Header set Content-Type text/css env=ASSETSCSS
    Header set Content-Encoding gzip env=ASSETSCSS
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(assets/.*)\.js $1\.js\.gz [L,ENV=ASSETSJS:true] [L]
    Header set Content-Type text/javascript env=ASSETSJS
    Header set Content-Encoding gzip env=ASSETSJS
</IfModule>

# Redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

使用我使用的 3 个转换器之一进行转换甚至都不起作用。 另一个都扔了这样的东西:

        rewrite ^/(?:data/)?(preview|sample|jpeg|image)/([0-9a-f]{2})([0-9a-f]{2})([$ /;
if ( ~ ""){
    set $rule_1 1$rule_1;
}
if ($rule_1 = "1"){
    rewrite / /;
}
if ( ~ ""){
    set $rule_2 1$rule_2;
}
if ($rule_2 = "1"){
    rewrite / /;
}
if (!-f $request_filename){
    set $rule_3 1$rule_3;
}
if ($rule_3 = "1"){
    rewrite /.* /index.php last;
}

但它仍然无法正常工作。 如果你能帮我解决一下就好了:)

【问题讨论】:

  • 您的第一个RewriteRule 不完整。

标签: apache .htaccess mod-rewrite nginx


【解决方案1】:

首先,你必须了解这个.htaccess做了什么

除了不完全重写之外,此.htaccess 会尝试提供预压缩资产(file.css.gz 而不是file.css)并将对不存在文件的请求重写为index.php

我会这样写:

location / {
    try_files $uri $uri/ /index.php;
}

location ^~ /assets/ {
    gzip_static on;
}

location ~ \.php$ {
    try_files $uri /index.php;
    # other PHP fastcgi stuff
}

参考:

【讨论】:

  • 我从来没有使用手动重写规则对此感到抱歉,而且由于我过去只使用过 apache,所以我很难熟悉 nginx。无论如何,我真的很感谢你的回应!不只是投反对票并继续滚动,因为他需要学习如何自己获得它……我正在学习,但如果没有人表明这个方法做得很好……
猜你喜欢
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
相关资源
最近更新 更多