nGinx和PHP-FPM下载index.php

时间:2016-02-13 17:19:04

标签: php nginx

我有以下配置来侦听端口80,但重定向到https。然后,要通过index.php文件路由所有内容,但是,当我转到页面时,它只会下载index.php文件或在主页上显示403 forbidden

有什么想法吗?

server {
    listen 80;
    server_name www.mydomain.com mydomain.com;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen 443;
    server_name www.mydomain.com;
    root /var/www/mydomain/public;

    ssl on;
    ssl_certificate mydomain.crt;
    ssl_certificate_key mydomain.key;
    ssl_session_timeout 5m;
    ssl_protocols SSLv3 TLSv1;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
    ssl_prefer_server_ciphers on;

    location / {
        if (!-e $request_filename){
            rewrite \.(jpeg|jpg|gif|png)$ /public/404.php break;
        }
        if (!-e $request_filename){
            rewrite ^(.*)$ /index.php break;
        }
    }

    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root /var/www/mydomain/public;
    }

    location ~ /\.ht {
        deny all;
    }
}

1 个答案:

答案 0 :(得分:0)

rewrite/break导致重写的URI在同一位置(location /)而不是location ~ \.php处理。请参阅rewrite文档。

location /块看起来很奇怪,有两个相同的if块。

您可以考虑将其拆分为两个块:

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

location ~ \.(jpeg|jpg|gif|png)$ {
    try_files $uri /public/404.php;
}

如果/public/404.php是所有404错误的默认处理程序,则可以改为使用error_page指令:

error_page 404 /public/404.php;

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

location ~ \.(jpeg|jpg|gif|png)$ {
    try_files $uri =404;
}

有关详情,请参阅locationtry_fileserror_page文档。

相关问题