草庐IT

php - nginx 本地网络服务器的所有权和权限

coder 2024-05-02 原文

我在设置本地 nginx 环境时遇到问题。我已经在网上阅读了无数的教程,所有内容似乎都让我的大脑有些困惑。

操作系统:OSX 10.11.4 El Capitan Nginx:1.8.1 PHP-FPM:5.5.31

目前我的web根目录下的文件目录如下:

/webserver
/webverver/phpinfo.php
/webserver/example
/webserver/example/index.php

我可以使用 curl 或在访问 localhost 的 Web 浏览器中访问默认的“欢迎使用 Nginx”页面。如果我然后浏览我得到文件的索引,但 PHP 文件将尝试下载而不是执行。如果我尝试访问我在 local.example.com 创建的示例站点(我已将其添加到我的主机文件中),那么我会使用 curl 和一个漂亮、简单的 ' 返回 403 Forbidden header 使用 Web 浏览器访问被拒绝。

我不太清楚文件权限和目录所有权,有人可以建议我应该如何配置所有内容吗?我被建议运行以下命令,但到目前为止它没有任何改变:

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

服务器/示例

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

这是在我的网络服务器根目录下的结果:

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)

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

最佳答案

案例 list :

  1. 确保 403 Forbidden 是由网络服务器而非应用程序脚本引起的。例如使 index.php 看起来像
echo 'hi there';

如果错误仍然存​​在,则可能是我们配置了错误的网络服务器。

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

还是这样

error_log /var/log/nginx/example.error.log notice

但是根据您的 nginx.conf,您已经这样做了,因此请检查日志文件以查找有关权限相关问题的信息。

通常你应该在那里找到问题的具体描述 - 文件无效权限,套接字无效权限或上游问题。

  1. 修正错误。这通常取决于我们在上一步中发现的内容。

一个。 Web 服务器上托管的文件权限错误。

1) 谁是谁 - 确定网站服务器用户(默认为 nginx)以及站点目录的所有者和组(/Users/nickcorin/webserver/example)。每个父目录(本身)应该(至少)对 nginx 用户(用户、nickcorin 和网络服务器)是可执行的 (--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 那样使文件可执行)

上游故障排除。检查你的上游 php 防火墙(如果有的话)是否正常 { server 127.0.0.1:9000;

注1。 “欢迎使用 Nginx” html 文档通常存储在/usr/share/中,需要授权。

注2。最好使用系统中的某个位置,您将为您的环境手动创建和设置所有必需的访问权限,而不是使用具有 700 权限的用户目录(并导致一些额外的步骤来设置权限相关的东西)。

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

关于php - nginx 本地网络服务器的所有权和权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36761211/

有关php - nginx 本地网络服务器的所有权和权限的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  4. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  5. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  6. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  7. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  8. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  9. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

  10. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

随机推荐