【问题标题】:NGINX rewrite configuration for another website in a sub directory triggers script download子目录中另一个网站的 NGINX 重写配置触发脚本下载
【发布时间】:2016-11-27 13:03:21
【问题描述】:

这是我原来的nginx配置

server {
    listen       80;
    server_name  my-site.com;
    return       301 http://www.my-site.com$request_uri;
}

server {
    listen       80;
    server_name  www.my-site.com;
    root         /var/www/sites/mysite/public_html;
    index        index.php index.html index.html;

    access_log   /var/log/nginx/mysite.access.log;
    error_log    /var/log/nginx/mysite.error.log;

    ssl off;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_read_timeout 180;
        fastcgi_buffers  16 16k;
        fastcgi_buffer_size  32k;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

然后我想在子文件夹中添加另一个站点让我们说 "my-site.com/another_app" 但是这个站点需要重写 url,例如:

my-site.com/another_app/api
my-site.com/another_app/home

变成

my-site.com/another-app/api.php
my-site.com/another-app/index.php?mod=home

我已经尝试像这样在 "location /" 中添加重写

location / {
    try_files    $uri $uri/ index.php;
    rewrite ^/another-app/api$ /another-app/api.php break;
    rewrite ^/another-app/([A-Za-z0-9-|_]+)/?$ /another-app/index.php?mod=$1 break;
}

但每次我尝试访问 "my-site.com/another-app/api" 时都会触发下载 php 脚本

我错过了什么吗?

【问题讨论】:

  • 尝试使用last而不是break(假设两个应用程序共享同一个文档根目录。详情请参阅this document
  • 我尝试使用 last 但仍然触发脚本 donwload

标签: php .htaccess nginx


【解决方案1】:

我还不能评论,所以我这样回答。

我只遇到过 css 和 js 等静态文件的问题:

我通过索引静态文件夹中的文件来修复它:

location /static/ {
    # My static files including the css reside inside my static
    # folder e.g. /static/css/style.css
    # autoindex on will index everything under the given location

    autoindex on;
 }

正如@Richard Smith 所说,尝试使用 last。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多