【问题标题】:Set root for PHP in nginx在 nginx 中为 PHP 设置 root
【发布时间】:2018-03-31 20:06:02
【问题描述】:

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

<?php 
die(print_r(getcwd(), TRUE)); 
?>

给我 /var/www/mydomain.com

我想将 mydomain.com 设置为 docroot,然后能够使用 '/' ex.

include '/css/style.css';

现在我必须使用

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;
    }
}

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

【问题讨论】:

  • 只需使用__DIR__ 获取您所在文件的绝对路径,并从中导航。
  • 我不想使用__DIR__ 当我有主机时我可以使用'/'
  • @Widziks 你的问题不清楚!你到底想要什么?

标签: php nginx


【解决方案1】:

由于你运行的是php-fpm,你可以在配置中设置chroot,并将其限制在某个目录下。更多详情请看下面的帖子

https://serverfault.com/questions/344538/php-fpms-chroot-and-chdir-directory

chroot 字符串

Chroot 到这个目录开始。该值必须定义为绝对路径。未设置此值时,不使用 chroot。

chdir 字符串

Chdir 到这个目录的开始。该值必须是绝对路径。默认值:当前目录或chroot时的/。

https://secure.php.net/manual/en/install.fpm.configuration.php

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

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

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 2015-10-31
    • 2012-07-20
    • 2010-11-15
    • 2011-05-14
    • 1970-01-01
    • 2012-04-04
    • 2023-03-27
    相关资源
    最近更新 更多