子目录中另一个网站的NGINX重写配置会触发脚本下载

时间:2016-11-27 13:03:22

标签: php .htaccess nginx

这是我原来的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" 但是这个网站需要重写网址,如:

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脚本

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我无法评论,所以我这样回答。

我只遇到像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。