简体   繁体   English

AWS EC2 NGINX上的PHP脚本不起作用,但下载了

[英]PHP script on AWS EC2 NGINX not working but download

I'm trying to set up my site in AWS ec2 using nginx. 我正在尝试使用nginx在AWS ec2中设置我的网站。 Here is my nginx.conf : 这是我的nginx.conf

include /etc/nginx/conf.d/*.conf;

index index.php index.html index.htm;

autoindex off;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  127.0.0.1;
    root         /usr/share/nginx/myproject;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    #location / {
    #}

    location = /index.php {
      rewrite ^(.*)$ /app/index.php break;
    }

    location / {
      if (!-e $request_filename){
        rewrite ^(.*)$ /app/index.php break;
      }
    }

    location ~ \.(secret|salt|engine|inc|po|sh|bat|cmd|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|inc)$ {
      deny all;
    }

    # redirect server error pages to the static page /40x.html
    #
    error_page 404 /404.html;
        location = /40x.html {
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files      $uri =404
        root           /usr/share/nginx/myproject;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

When I access the public DNS, I got download instead of running the PHP script. 当我访问公共DNS时,我得到了下载,而不是运行PHP脚本。 Is there any problem in my nginx config? 我的nginx配置有问题吗?

Your rewrite...break statements should be rewrite...last as the PHP file needs to be processed in a different location. 您的rewrite...break语句应该被rewrite...last因为PHP文件需要在其他位置处理。 See this document for details. 有关详细信息,请参见此文档

For example: 例如:

location / {
    if (!-e $request_filename){
        rewrite ^ /app/index.php last;
    }
}

However, the above functionality is usually implemented as: 但是,以上功能通常实现为:

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

Other issues include: 其他问题包括:

  • a missing ; 失踪; in the location ~ \\.php$ block following the try_files statement. try_files语句之后的location ~ \\.php$块中。

  • the root statement in the location ~ \\.php$ block should be deleted, as it is unnecessary. location ~ \\.php$块中的root语句应删除,因为这是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM