【问题标题】:Issues converting .htaccess to Nginx configuration for PHP scripts将 .htaccess 转换为 PHP 脚本的 Nginx 配置的问题
【发布时间】:2012-03-05 01:48:28
【问题描述】:

我正在尝试转换一个 .htaccess 文件,以便它可以在我的 Nginx 服务器上运行。

我有两个主要问题:

1。我不确定如何转换某些行

2。 Nginx的配置代码不知道放哪里

以下是我在转换时遇到问题的行:

 Options All -Indexes

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 RewriteRule ^(.*)$ index.php?page_request=$1 [QSA,L]

我尝试了一些在线转换器,但它们似乎不起作用。

另外,一旦我转换了线条,我应该把它们放在哪里?我假设我需要将它们放在/etc/nginx/sites-available/mysite.conf 文件中,但是在该文件中,我是否只是将它们放在server {} 块中?

非常感谢您能给我的任何帮助。

编辑:这是 conf 文件

 server {
    listen 0.0.0.0:80;
    server_name www.subdomain.domain.tld subdomain.domain.tld;
    access_log /var/log/subdomain.domain.tld.log;

    location / {
        root /srv/www/subdomain.domain.tld/public_html;
        index index.html index.php;
        }
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /srv/www/subdomain.domain.tld/public_html$fastcgi_script_name;
        }

       try_files $uri $uri/ /index.php?page_request=$uri;
}

【问题讨论】:

    标签: php .htaccess nginx


    【解决方案1】:

    更新

    做一些 nginx 研究,遇到了这个,试一试,看看它是否像你期望的那样工作:

    location / {
        # other code below this line
        try_files $uri $uri/ /index.php?page_request=$uri;
    }
    

    基本上try_files就是if语句,它先尝试常规文件,如果找不到它,它作为一个目录尝试,如果它找不到它发送它到最后一个参数。


    您是否尝试过使用类似的服务:http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

    它输出:

    #ignored: condition 0
    #ignored: condition 1
    if ($rule_0 = "2"){
    rewrite /RewriteRule ^/(.*)$;
    }
    

    对于您提供的数据。

    你把它们放在里面:

    server {
    
    }
    

    由于这不是针对特定的location,您只需将其放在默认内容之后即可。有操作的顺序,所以是的。如果您有多个位置类型位,它可以在一个位置内。对于 nginx,我可能会将其放在 .php 位置定义下。

    【讨论】:

    • 是的,我试过这个网站,但这段代码不起作用。事实上,注释的“忽略:条件 0”意味着转换忽略了我的代码中的行。运行此代码会产生错误:nginx: [emerg] invalid variable name in /etc/nginx/sites-enabled/atr3.com.conf:38.
    • 你能发布 atr3.com.conf 吗?这样可以更轻松地查看可能出现的问题。
    • 我刚刚编辑了我的问题,将其添加进去。(subdomain.domain.tld 代替了实际数据)
    • 另外,我也知道转换器的代码不能工作,因为它需要将字符串作为 page_request $_GET 变量发送给 PHP。
    • 嗯...现在看来这很有意义,但我在尝试此操作时仍然遇到 404 错误。
    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 2021-10-21
    • 2019-05-19
    • 2015-04-11
    • 2018-07-10
    • 2017-08-02
    • 2011-05-07
    相关资源
    最近更新 更多