簡體   English   中英

Nginx & PHP 與附加目錄

[英]Nginx & PHP with additional directories

我有一個 Nginx 服務器,它將托管一些從 Apache 服務器遷移的網站。

我一直試圖弄清楚的一件事是如何包含其他目錄以查看何時調用文件。

在 Apache 中,我可以在虛擬主機中包含以下內容:

php_value include_path /sites/web-test1/vendor/webtoolkit/src:/sites/web-test1/private:/usr/share/php

這在 Apache 中運行良好。 但在 Nginx 中,我嘗試使用

包括/sites/web-test1/vendor/webtoolkit/src;

但這不起作用。 任何人都可以闡明這是如何執行的嗎?

因此,根據建議,我在我的虛擬主機中包含了以下內容:

fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/vendor/webtoolkit/src/includes";
fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/vendor/";
fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/private/";
fastcgi_param  PHP_VALUE  "include_path=/usr/share/php/";

請注意,我在語句中嘗試了使用和不使用“includes”目錄的第一行,但它似乎沒有任何區別。 第一行是調用下面顯示的文件所需要的。

如果它有所不同,則文件使用以下行:

<?php include('includes/emailpriv.inc.php'); ?>

但我仍然不斷收到同樣的錯誤:

php: PHP Warning:  include(includes/emailpriv.inc.php): failed to open stream: No such file or directory in /git/web-test1/public/emailpriv.html on line XX
php: PHP Warning:  include(): Failed opening 'includes/emailpriv.inc.php' for inclusion (include_path='/usr/share/php') in /git/web-test1/public/emailpriv.html on line XX

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 102400;

events {
    worker_connections 100000;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        server_names_hash_max_size 512;
        server_names_hash_bucket_size 128;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

        log_format dm '$host - $remote_addr - $remote_user [$time_local] '
                      '"$request" $status $bytes_sent '
                      '"$http_referer" "$http_user_agent"';

        access_log /nginx/log/nginx/access.log dm;
        error_log /nginx/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

和虛擬主機:

server {
    server_tokens off;
    client_max_body_size 100M;
    server_name ws2.xxxxxxxxxxxx.com;
    listen 443 ssl;

    access_log /nginx/log/nginx/test1.access.log;
    error_log  /nginx/log/nginx/test1.error.log;
    
    root /git/web-test1/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.html /index.php?$args;
}

    location ~ \.(php|html|htm)$ {
        try_files $uri =404/
    include snippets/fastcgi-php.conf;
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php7.4-fpm.sock; # regular pool
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param SERVER_NAME $host;
    fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/vendor/webtoolkit/src/includes";
    fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/vendor/";
    fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/private/";
    fastcgi_param  PHP_VALUE  "include_path=/usr/share/php/";
}
    ssl_certificate /etc/letsencrypt/live/ws2.xxxxxxxxxxxx.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ws2.xxxxxxxxxxxxx.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = ws2.xxxxxxxxxxxxxxx.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name ws2.xxxxxxxxxxxx.com;
    return 404; # managed by Certbot

}

在中添加index.php文件(NGINX配置文件會在/etc/nginx/目錄下找到)

並更改相同的瀏覽器路徑。 重新加載 nginx,重新加載 php-fpm

經過一番挖掘和試驗,我發現問題在於我如何格式化虛擬主機。 如上所述,我用自己的“fastcgi_param PHP_VALUE”分隔了每個文件夾

但是當你這樣做時,它會覆蓋以前的 PHP_VALUE。 所以正確的格式如下:

fastcgi_param PHP_VALUE "include_path=/sites/web-test1/vendor/webtoolkit/src/;include_path=/sites/web-test1/vendor/;include_path=/sites/web-test1/private/;include_path=/usr/share/ php/";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM