nginx本地网页服务器的所有权和权限

5
我有一些问题在设置本地nginx环境时遇到了困难。我已经阅读了很多在线教程,但每个都让我感到头昏脑胀。
操作系统: OSX 10.11.4 El Capitan Nginx: 1.8.1 PHP-FPM: 5.5.31
目前我的网站根目录文件夹如下:
/webserver
/webverver/phpinfo.php
/webserver/example
/webserver/example/index.php

我可以使用curl或在本地主机上访问浏览器来访问默认的“欢迎使用Nginx”页面。如果我浏览文件,那么PHP文件将尝试下载而不是执行。如果我尝试访问我已经创建的示例站点local.example.com(我已将其添加到我的hosts文件中),则使用curl会返回403禁止头部返回,而使用网络浏览器则会显示漂亮简单的“拒绝访问”。

我对文件权限和目录所有权不是很了解,有人能告诉我应该如何配置吗?我被建议运行以下命令,但目前为止没有改变:

sudo chmod -R 755 /Users/nickcorin/webserver

我的错误日志中没有任何日志,除了“信号已启动”的日志。

这是我目前的配置:

nginx.conf

#user nobody;
worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /Users/nickcorin/webserver;
        autoindex on;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    include servers/*;
}

servers/example

upstream php {
    server 127.0.0.1:9000;
}

server {
    listen 80;

    root /Users/nickcorin/webserver/example;
    server_name local.example.com;

    index index.php index.html index.htm;
    autoindex on;

     location ~ \.php$ {
            try_files  $uri  $uri/  /index.php?$args ;
            index  index.html index.htm index.php;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_intercept_errors on;
            include fastcgi_params;
        }
}

**编辑 - NGINX文件夹权限和进程所有者**

我在我的日志文件夹/usr/local/var/nginx上运行了ls -la命令,结果如下:

drwxr-xr-x  4 nickcorin  admin   136 Apr 20 23:53 .
drwxr-xr-x  5 nickcorin  admin   170 Apr 20 21:47 ..
-rw-r--r--  1 root       admin  4718 Apr 21 08:06 access.log
-rw-r--r--  1 nickcorin  admin   480 Apr 21 10:28 error.log

这是我的Web服务器根目录的结果:

drwxr-xr-x   4 nickcorin  staff   136 Apr 22 12:23 .
drwx-----x+ 54 nickcorin  staff  1836 Apr 22 10:01 ..
drwxr-xr-x   3 nickcorin  staff   102 Apr 20 22:14 example
-rw-r--r--@  1 nickcorin  staff    23 Apr 19 11:58 info.php

这是运行ps aux | grep nginx命令的结果:
root              756   0.0  0.0  2466616    480   ??  Ss   12:24PM   0:00.00 nginx: master process nginx
nickcorin         759   0.0  0.0  2445080    820 s000  S+   12:24PM   0:00.00 grep nginx
nobody            757   0.0  0.0  2475832   1044   ??  S    12:24PM   0:00.00 nginx: worker process

**编辑#2 - 虚拟主机配置文件 **

我已经解决了我的问题,现在一切都运行顺利。我不得不编辑我的虚拟主机配置文件如下:

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name local.example.com;

    root /Users/nickcorin/webserver/example;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

我使用以下两行代码来修复我的权限问题:
sudo chown -R nickcorin /Users/nickcorin/webserver (Web Server Root)
sudo chmod 755 /Users/nickcorin/webserver/example (Virtual Host Root)

感谢所有帮助我解决问题的人 :)

你的php-fpm池中的用户是否与你的网页文件的用户/组所有者匹配? - Hamed Nemati
你完全开启了一个新的思路,我对nginx非常陌生,我不知道我需要做这样的事情?我只是让一切都保持php-fpm默认设置。ps aux | grep php-fpm显示我的用户“nickcorin”处理进程。 - Nick Corin
ps aux | grep php-fpm 表示在 php-fpm 的池配置中指定 listen.ownerlisten.group 属性。确保池配置中的 usergroup 与您的文件权限匹配。编辑默认池配置,例如:/etc/php5/fpm/pool.d/www.conf 或将其复制到新池中,例如:/etc/php5/fpm/pool.d/mysite.conf 并通过 listen = 127.0.0.1:9001 让它监听另一个端口。 - Hamed Nemati
2个回答

4
我遇到的问题是虚拟主机配置文件和Web服务器根目录权限。以下是我目前正常工作的配置设置,供其他遇到类似问题的人参考:

nginx.conf

user yourusername staff;
worker_processes  1;

error_log  logs/error.log;
error_log   logs/error.log warn;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /Users/yourusername/webserver;
            autoindex on;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    include servers/*;
}

servers/example

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name local.example.com;

    root /Users/yourusername/webserver/example;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

一旦这些设置正确,我就运行以下命令来修复 Web 服务器中的权限错误:

sudo chown -R yourusername /Users/yourusername/webserver
sudo chmod 755 /Users/yourusername/webserver/example

不要忘记将 127.0.0.1 local.example.com 添加到您的 hosts 文件中。


3

案例检查清单:

  1. 确保403 Forbidden是由Web服务器而不是应用程序脚本引起的。例如,将index.php修改为以下内容:
echo 'hi there';

如果错误仍然存在,则可能是我们配置的Web服务器出现了问题。

  1. 找出Web服务器生成的权限被拒绝错误的原因。您可以暂时将此指令添加到您的servers/example配置文件中(可能在server_name之后):
error_log /var/log/nginx/example.error.log warn

甚至更多的是
error_log /var/log/nginx/example.error.log notice

根据您的nginx.conf配置文件,您已经完成了这个步骤,所以请检查日志文件以查找与权限相关的问题。

通常,您应该在日志中找到具体的问题描述 - 文件无效权限,套接字无效权限或上游出现问题。

  1. 修复错误。这通常取决于我们在前一步找到了什么。

a. 网站服务器上托管文件的权限设置不正确。

1) 查看网站服务器用户(默认为nginx)和站点目录(/Users/nickcorin/webserver/example)的所有者和组。每个父目录(包括其本身)都应该对nginx用户(即Users、nickcorin和webserver)可执行(--x)。

2) example目录及其所有内容也应该是可读的(r-x)。要实现这一点,可以使用以下命令:

# cd example
# find . -type d | xargs chmod 755
# find . -type f | xargs chmod 644

这样做并不能像sudo chmod -R 755 /Users/nickcorin/webserver一样使文件可执行。

b. 上游故障排除。检查防火墙(如果有的话)是否与上游php { server 127.0.0.1:9000; }兼容。

注意1。“欢迎来到Nginx”HTML文档通常存储在/ usr / share /中,需要授予必要的权限。

注意2。最好使用系统中的某个位置,您可以手动创建并设置所有所需访问权限的环境,而不是使用带有700权限的用户目录(并导致一些附加步骤设置权限相关的内容)。

注意3。请记住,当我们在目录中没有索引文件时,也会响应“403 Forbidden”。


1
哇!谢谢你的回答!:D
  1. 我的index.php已经包含了phpinfo() echo。
  2. 我的error.log只包含了nginx信号启动的条目。
这应该指向像你说的权限错误,我使用了ps aux | grep nginx,'root'处理了nginx主进程,但是' nobody'处理了工作进程。我将尝试更改权限!非常感谢你的回答。第三点,我确实看到了这个 - 我在网站根目录中有一个index.php,并已打开自动索引。
- Nick Corin
运行chmod命令没有改变任何东西,我有一种感觉与用户和组有关。'Nobody'处理nginx进程。当我右键单击“example”文件夹并单击获取信息时,它说“nickcorin”可以读/写,工作人员可以读取,每个人都可以读取。就是这样。 - Nick Corin
如果您修复error_log及其目录的文件权限,则更容易找到和解决问题。我猜如果工作进程在“nobody”用户下运行,则可能会出现日志记录问题。但我不确定。您必须检查/var/log/nginx或任何存储nginx日志的位置的权限。如果您想立即使事情正常化-可以在nginx日志目录上设置777。但是,如果您想了解发生了什么,请检查是否存在日志记录问题! :) 尝试发布一些nginx日志文件夹的ls -la列表。 - A.Efremov
谢谢@A.Efremov!我在问题中列出了一些终端输出。我不认为我完全理解权限和所有权的概念。 - Nick Corin
所以,结果我最终解决了我的错误。我搜索了所有能找到的网络教程并编辑了我的服务器配置文件。我不确定它是如何解决我的错误的,但这将是我未来配置虚拟主机的方式。感谢您的帮助 :) 我会在问题中发布我的配置文件。 - Nick Corin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接