7
回答
nginx不能正常访问,提示权限不足或文件不存在的错误!
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

今天通过yum安装了新的nginx(v1.6.2),修改了配置文件中的user,root,server_name,index,其他都是系统默认配置,浏览器访问时一直提示错误,权限不足或者文件不存在的错误(其中文件肯定是存在的)。

user admin;

// 或者:user admin admin;

// 同网站根目录用户一致

root /tmp/html;

server_name test1.com test2.com;

index index.php index.html;

无论设置nginx的user为root|admin|nginx|nobody,还是设置网站根目录的用户和权限(0777|0755|0551||root|admin|nginx),都不行,一样错误!郁闷啊!!

根据(nginx -V)中prefix的设置(/usr/share/nginx),如果将网站根设置到(/usr/share/nginx)目录下面,可以访问html,jpg等类型的静态文件,但在访问php时(初始化thinkphp),提示”应用目录[./Application/]不可写“的错误!弄不明白的是nginx到底使用哪种权限来读取文件和目录,哪怕是设置0777也不行!

php-fpm的user和group也换过了!

愁到喽!!!

# nginx -V
nginx version: nginx/1.6.2
built by gcc 4.9.1 20140930 (Red Hat 4.9.1-11) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'



# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user	admin;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    index   index.html;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        server_name  test1.com test2.com;
        #root         /usr/share/nginx/html;
	root		/usr/share/nginx/thinkphp_3.2.3_full;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

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



# cat /etc/nginx/conf.d/php-fpm.conf 
# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server 127.0.0.1:9000;
}



# cat /etc/nginx/default.d/php.conf 
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html;

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass   php-fpm;
}



# uname -an
Linux minicooper 3.18.3-201.fc21.x86_64 #1 SMP Mon Jan 19 15:59:31 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux






举报
自由男爵
发帖于3年前 7回/38K+阅
共有7个答案 最后回答: 3年前

1、查看错误日记 /var/log/nginx/error.log; 以定位问题

2、检测nginx是否已使用了你配置的文件(可以故意出错测试)

3、确保目录/tmp/html;  有执行权限

2015/03/20 16:09:07 [error] 15278#0: *1 open() "/tmp/thinkphp_3.2.3_full/index.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /index.html HTTP/1.1", host: "localhost"

注:index.html文件是存在的。

2015/03/20 16:09:37 [error] 15278#0: *1 open() "/tmp/thinkphp_3.2.3_full/asdfasdfsdaf.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /asdfasdfsdaf.html HTTP/1.1", host: "localhost"

注:asdfasdfsdaf.html文件是不存在的。

# systemctl restart nginx
Job for nginx.service failed. See "systemctl status nginx.service" and "journalctl -xe" for details.

# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: failed (Result: exit-code) since 五 2015-03-20 16:11:14 CST; 18s ago
  Process: 15308 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 15275 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 15313 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)
 Main PID: 15277 (code=exited, status=0/SUCCESS)

3月 20 16:11:14 minicooper nginx[15313]: nginx: [emerg] invalid number of arguments in "user" directive in /etc/nginx/nginx.conf:6
3月 20 16:11:14 minicooper nginx[15313]: nginx: configuration file /etc/nginx/nginx.conf test failed
3月 20 16:11:14 minicooper systemd[1]: nginx.service: control process exited, code=exited status=1
3月 20 16:11:14 minicooper systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
3月 20 16:11:14 minicooper systemd[1]: Unit nginx.service entered failed state.
3月 20 16:11:14 minicooper systemd[1]: nginx.service failed.



注:这是将配置文件中user后面的分号去掉后的情况;

# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: active (running) since 五 2015-03-20 16:13:40 CST; 1s ago
  Process: 15308 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 15332 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 15330 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 15334 (nginx)
   CGroup: /system.slice/nginx.service
           ├─15334 nginx: master process /usr/sbin/nginx
           └─15335 nginx: worker process

3月 20 16:13:40 minicooper nginx[15330]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3月 20 16:13:40 minicooper nginx[15330]: nginx: configuration file /etc/nginx/nginx.conf test is successful



注:这是正常情况;

问题解决了,原因在于:我使用的系统是fedora 21,系统默认的服务管理程序是systemctl,原来是这个有问题,如果在命令行中直接使用nginx启动服务,这一切正常,使用相同方式启动php-fpm后,php应用也正常。看来是分析问题的方向有问题了,只是一个服务管理程序而已,使用ps和netstat查看进程和端口,都正常,也就没有多想,正是害人不浅啊!!

systemctl enable nginx

systemctl restart nginx

systemctl enable php-fpm

systemctl restart php-fpm

之后就正常了。

可是还存在一个问题:

# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.


# systemctl disable nginx
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.


这时候‘/etc/systemd/system/multi-user.target.wants/nginx.service’文件已经不存在了,实际查看也是不存在了,重启nginx后还是访问正常,看来不仅仅是创建link那么简单。

今天使用nginx又出现了权限不足的问题,检查所有配置,一切正常,按照上述方法检查systemctl status nginx,也都OK,无意中想到了selinux,就随手敲了关闭命令,没想到nginx居然正常了,检查一看没想到系统配置文件中还是打开的(记忆里好像是关掉的)。

上次出问题,除了权限不足还有文件不存在的错误,而今天是只是权限不足的问题,所以导致错误的源头应该两者都有,现在想想上次好像也使用了selinux的关闭命令,然后才是systemctl的问题,这次正好相反。

顶部