我在Ubuntu 16服务器上安装了nginx和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脚本仍在下载而不是被执行。
我也试图清除缓存,但它没有帮助。
配置中我还缺少什么?
答案 0 :(得分: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中所述。