0

我在 digitalocean 有一个 ubuntu 服务器,我试图让 php 工作。我已经成功地用 nginx 创建了一个子域,尽管 nginx 一直在下载 php 文件,而不是为它们提供服务。这是我的默认设置:

server {
    listen 80;
    listen [::]:80;
    index index.html index.php;
    server_name _;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

我试图将以下内容添加到默认的 nginx 文件中,但没有成功:

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

完成:

server {
    listen 80;
    listen [::]:80;
    index index.html index.php;
    server_name _;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

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

我开始认为“location / {”是针对每个内容的,还是某处存在错误配置?如果我将 html 文件添加到子域,它工作得非常好,然后 Nginx 正确地为它们提供服务,但不是 php。

我的 nginx 配置文件如下所示:

server {
    listen 80;
    index index.php index.html;
    server_name sub.example.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /home/simonduun/sub;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ ^/wp-json/ {
        rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
    }
}
4

0 回答 0