在nginx中为PHP设置根目录

在nginx中为PHP设置根目录,php,nginx,Php,Nginx,如何在nginx(1.12.2)上为php设置绝对docroot 现在我必须使用 include '/var/www/mydomain.com/css/style.css'; 我的nginx配置 server { listen 80; server_name mydomain.com; root /var/www/mydomain.com/; index index.php index.html index.htm; error_page 404 /4

如何在nginx(1.12.2)上为php设置绝对docroot

现在我必须使用

include '/var/www/mydomain.com/css/style.css';
我的nginx配置

server {
    listen 80;
    server_name mydomain.com;
    root /var/www/mydomain.com/;
    index index.php index.html index.htm;

    error_page 404 /404.html;

   location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME /var/www/mydomain.com$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }

    location / {
        try_files $uri $uri/ =404;
    }
}

将所有站点文件设置在public或html文件夹(/var/www/mydomain.com/public)中有什么好的做法吗?

由于您正在运行
php fpm
,您可以在配置中设置
chroot
,并将其限制在某个目录中。有关更多详细信息,请参阅下面的线程

chroot字符串

Chroot在开始时指向此目录。此值必须定义为绝对路径。如果未设置此值,则不使用chroot

chdir字符串

Chdir在开始时指向此目录。此值必须是绝对路径。默认值:当前目录或/when chroot

因此,您将在php fpm配置中添加以下内容

chroot=/var/www/mydomain.com
chdir=/

我想将mydomain.com设置为docroot,然后可以使用“/”例如。
这不是它真正的工作原理
/
是根文件夹,而不是web服务器的web根目录。只需使用
\uuuu DIR\uuuuu
即可获取您所在文件的绝对路径,并从中导航。我不想在托管时使用
\uuuu DIR\uuuu
,我可以使用“/”@Widziks您的问题不清楚!你到底想要什么?
server {
    listen 80;
    server_name mydomain.com;
    root /var/www/mydomain.com/;
    index index.php index.html index.htm;

    error_page 404 /404.html;

   location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME /var/www/mydomain.com$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }

    location / {
        try_files $uri $uri/ =404;
    }
}
chroot=/var/www/mydomain.com
chdir=/