2017-08-11 41 views
1

我已經在Ubuntu 16服務器上安裝了nginx和PHP7。nginx - PHP腳本(PHP7)仍在下載,未執行

PHP7在這裏安裝:

which php 
/usr/bin/php 

,這裏是我的nginx的配置文件:

upstream unicorn { 
    server unix:/home/deployer/apps/myapp/shared/sockets/unicorn.myapp.sock; 
} 

server { 
    listen 443 ssl; 
    server_name myapp.com; 

    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot 
    ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot 
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot 
    ssl_dhparam /etc/ssl/certs/dhparam.pem; 
    return 301 https://www.myapp.com$request_uri; 
} 

server { 
    #listen 80 default deferred; 

    listen 443 ssl; # managed by Certbot 
    server_name www.myapp.com; 
    root /home/deployer/apps/myapp/current/public; 

    location ^~ /blog { 
    alias /home/deployer/blog; 
    index index.php; 
    try_files $uri $uri/ /blog/index.php?$args; 
    #rewrite ^/blog/(.*)+$ /blog/index.php?$1; 
    } 

    location ^~ /assets/ { 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    location ~ ^/(robots.txt|sitemap.xml.gz)/ { 
    root /home/deployer/apps/myapp/current/public; 
    } 

    try_files $uri/index.html $uri @unicorn; 
    location @unicorn { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-Forwarded-Proto https; # added because of infinite looping 
    proxy_redirect off; 
    proxy_pass http://unicorn; 
    proxy_read_timeout 120; # added for test purposes 
    } 

    location ~ \.php$ { 
    include snippets/fastcgi-php.conf; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 

    error_page 500 502 503 504 /500.html; 
    client_max_body_size 4G; 
    keepalive_timeout 10; 

ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot 
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot 
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot 
ssl_dhparam /etc/ssl/certs/dhparam.pem; 

    if ($scheme != "https") { 
     return 301 https://$host$request_uri; 
    } # managed by Certbot 


    # Redirect non-https traffic to https 
    # if ($scheme != "https") { 
    #  return 301 https://$host$request_uri; 
    # } # managed by Certbot 
} 

我已經嘗試了許多不同的方法來正確地配置了PHP,但仍無法找到合適的辦法。 PHP腳本仍在被下載而不是被執行。

我也嘗試清除緩存,但它沒有幫助。

我在配置中還缺少什麼?

回答

1

您當前的location ~ \.php$的根目錄不正確,無法看到以/blog開頭的任何URI。由於您有兩個根,並且在前綴location上使用^~修飾符,因此您需要爲PHP使用嵌套的location塊。例如:

location ^~ /blog { 
    root /home/deployer; 
    index index.php; 
    try_files $uri $uri/ /blog/index.php?$args; 

    location ~ \.php$ { 
     ... 
    } 
} 

alias指令是不必要的和低效的是location的值是一樣的alias值的末尾。改爲使用root指令 - 如手冊here中的建議。