【问题标题】:Nginx how to run site from another cataloge?Nginx 如何从另一个目录运行站点?
【发布时间】:2014-01-06 08:13:59
【问题描述】:

我尝试在 nginx 上运行我的网站,所以在前面我做了一些配置:

server {
listen 80;
server_name domain.com;
root /var/www/domain.com/www;
index index.php;

# Use FastCGI with PHP-FPM for all things PHP
location ~ .php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME /var/www/domain.com/www$fastcgi_script_name;
    include fastcgi_params;
}
}

这没关系。问题是当我尝试运行管理区域时。管理面板位于子目录 /var/www/domain.com/www/admin 中,并且有一个 index.php 文件。在 apache 中,我只是放了另一个带有重写规则的 .htaccess 文件:

RewriteEngine on

RewriteRule !\.(js|ico|gif|jpg|png|css|htc)$ index.php

一切正常,但如何在 nginx 中做到这一点?

我的网站是用前台和管理区域构建的。当您转到 domain.com/some_url.html 并且管理区域在 domain.com/admin/some_url.html 时运行前面。目录结构是这样的:

site/
    /module/
   /public_html/
             index.php
             /admin/
                  index.php

因此,当您运行 domain.com/admin/ 时,所有内容都应提交到 /admin/index.php 但现在当我转到 domain.com/admin/ 时会显示管理区域,但是当我单击某些链接时,所有流量都是提交到前面的 index.php 文件。
最好的问候!

【问题讨论】:

    标签: .htaccess mod-rewrite nginx


    【解决方案1】:

    好的,我明白了,

    试试这个

    server {
    listen 80;
    server_name domain.com;
    root /var/www/domain.com/www;
    
        location /admin/ {
          rewrite ^ /index.php?$request_uri;
        }
    
       # Use FastCGI with PHP-FPM for all things PHP
       location ~ .php$ {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_param SCRIPT_FILENAME /var/www/domain.com/www$fastcgi_script_name;
          include fastcgi_params;
       }
    }
    

    【讨论】:

    • 感谢您的回答。是的,我知道在 nginx 中没有 htaccess 文件。您的代码没有帮助,因为当我转到 domain.com/admin/some_link.html 时,请求转到主 index.php,而不转到 admin/index.php。有什么办法吗?路径中的 /admin/ 应视为目录。也许我应该为管理区域创建一个子域?最好的问候。
    • 我想我不明白你的问题,如果你想重写,你能用例子更新问题,你想重写什么以及你想重写的地方吗?
    • index index.php; 不会重写,它只是告诉 Nginx 在有人访问文件夹时要加载哪个文件。例如:如果你在浏览器中输入domain.com/admin/,如果你设置了nginx加载index.php,Nginx会加载domain.com/admin/index.php,如果没有,Nginx会显示Forbidden错误。要重写你必须使用rewrite 而不是index index.php;。更新问题,我可以帮忙。
    • 非常感谢您的帮助!!你的脚本需要一点点正确,但这是我需要的!正确重写: location /admin { rewrite ^ /admin/index.php?$request_uri; }
    猜你喜欢
    • 2018-02-24
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多