【问题标题】:Nginx websites isolationNginx 网站隔离
【发布时间】:2016-02-18 09:31:39
【问题描述】:

我遵循了很多 nginx 指南,但没有一个能让我满意。

我的目标是创建两个具有不同文件权限的网站,因此我确信它们是永久隔离的。

例如。 /var/www/site1 与所有者:site1 和组:site1/var/www/site2 与所有者:site2 和组:site2,这两个目录都有 770 权限。

我的 nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {

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

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


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


include /etc/nginx/sites-enabled/*;

}

我的 /etc/nginx/sites-available/site1:

server {
    listen 80;

    root /var/www/site1;
    index index.php index.html index.htm;

    server_name www.site1.org site1.org;

    access_log /var/log/nginx/access2.log;
        error_log /var/log/nginx/error2.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
        #try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/site1;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm-site1.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

...site2 也一样。

最后是我的 /etc/php5/fpm/pool.d/site1.conf:

[site1]
user = site1
group = site1
listen = /var/run/php5-fpm-site1.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

...site2 也一样。

只要 www-data 用户对 /var/www/site1 和 /var/www/site2 具有文件权限,一切都会按预期进行。当我使用 770 将 site1:site1 设置为 owner:group 时出现问题,然后我在日志中得到“13: Permission denied”。

这个想法是只有 php-fpm 必须拥有网站文件,因为 nginx 只是通过套接字与 php-fpm 通信,而 php-fpm 是操作文件的进程。为什么连 Nginx 都应该拥有它们?

【问题讨论】:

    标签: php nginx


    【解决方案1】:

    Nginx 可以与您想要的任何(受信任的)用户一起运行,包括这些文件的所有者......但是,只要 nginx 用户对两个站点都具有权限,您就不能完全隔离两个站点。

    如果您必须为两者运行 nginx,则必须具有虚拟化 (vps) 才能完美隔离它们(尽管“完美”这个词是相对的)。

    如果您可以为另一个站点使用另一台服务器,除了让每个用户作为该用户运行 php 之外,您还可以将 2 个不同的网络服务器放在不同的端口中,例如 litespeed 或 lighttpd。

    因此,您将为用户 1(并与用户 1 一起运行)使用 nginx + fpm,为用户 2 使用 lighttpd + fpm(与用户 2 一起运行)。 文件应位于每个其他用户的主目录中,并具有适当的权限以阻止其他用户的任何访问。

    然后,您可以将 nginx + lighttpd 放在 haproxy 后面,并根据主机名将请求路由到 nginx 或 lighttpd,这反过来将为每个用户启动 fpm,只提供来自该用户的文件。

    https://seanmcgary.com/posts/haproxy---route-by-domain-name
    

    但无论哪种方式,我认为最好的途径是虚拟化。

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 2012-08-26
      • 2016-04-28
      相关资源
      最近更新 更多