0

I'm trying to set up my site in AWS ec2 using nginx. Here is my 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. Is there any problem in my nginx config?

1 Answer 1

1

Your rewrite...break statements should be rewrite...last as the PHP file needs to be processed in a different location. 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.

  • the root statement in the location ~ \.php$ block should be deleted, as it is unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.