.pem文件_nginx访问php动态文件为下载状态的问题

Mac brew nginx多站点配置访问php动态文件root目录无效(被重置)

一、问题环境

1、本机环境

macOS 版本:10.15.1 Homebrew 2.2.6

2、测试文件路径

cd /Users/zaneli/WorkSpace/test

其中 aabb.php index.php 为php动态文件

abc.html index.html test.html 为html静态文件

3、nginx.conf 所在目录

cd cd /usr/local/etc/nginx

nginx.conf 配置文件内容

#user  nobody;

worker_processes  1;

error_log  /Users/zaneli/WorkSpace/logs/nginx/error.log;

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

#pid        logs/nginx.pid;
pid        /usr/local/var/run/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  0;
    keepalive_timeout  65;

    #gzip  on;

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
#   server {
#        listen       8099;
#        server_name  html.test;
#       root   /Users/zaneli/WorkSpace/test;

#        location / {
#            index  index.html index.htm;
#        }

#    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

4、多站点servers 配置所在目录

/usr/local/etc/nginx/servers

站点localhost.conf配置内容

server {
    #    listen       8080;
        listen       8066;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        #    root   html;
            root   /Users/zaneli/WorkSpace/test;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
#        location /index.php {
#            root           html;

         root   /Users/zaneli/WorkSpace/test;  # 划重点:这地方得再配置一下!

            root   /Users/zaneli/WorkSpace/test;
            fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
#            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }

站点test.conf配置内容

server {
    #    listen       8080;
        listen       8088;
        server_name  local.test;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        access_log  /Users/zaneli/WorkSpace/logs/test/access.log;
        error_log   /Users/zaneli/WorkSpace/logs/test/error.log;

        location / {
    #        root   html;
            root   /Users/zaneli/WorkSpace/test;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
#            root           html;

             root   /Users/zaneli/WorkSpace/test;  # 划重点:这地方得再配置一下!

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
#            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
            include        fastcgi_params;

#           include        fastcgi.conf;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }

5、站点访问情况

9ff08c9c26365745c400c2ca1d785a6c.png

二、具体问题及时下解决方式

1、每次开机后,执行 nginx -t,出现

dbf12153e85eed80186d0e6dcd21af9f.png
cd /usr/local/etc/nginx/ zaneli@bogon nginx % nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied) nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

解决方式一、

cd /usr/local/var/run
sudo chown -R $(whoami) /usr/local/var/run
sudo chmod -R 700 /usr/local/var/run

方式二、

sudo nginx -t

2、每次手动删除nginx日志文件后,执行 nginx -t,出现

e5d2c7ab82769493072a6399dbb0011a.png
nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: [emerg] open() "/Users/zaneli/WorkSpace/logs/nginx/error.log" failed (13: Permission denied) nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

解决方式

sudo chown zaneli:staff error.log
chmod 777 error.log

3、执行 nginx -s reload,出现

7804661208795fee431b1268304bbc54.png
nginx -s reload nginx: [alert] kill(1172, 1) failed (1: Operation not permitted)

解决方式

sudo nginx -s reload

4、nginx默认docroot

Docroot is: /usr/local/var/www
软链接到:/usr/local/Cellar/nginx/1.17.8/html

5、常用命令组

ps aux | grep nginx
sudo killall nginx
cd /usr/local/Cellar/nginx/1.17.8/bin zaneli@bogon bin % sudo ./nginx
/usr/local/Cellar/nginx/1.17.8/bin/nginx -t
/usr/local/Cellar/nginx/1.17.8/bin/nginx -c /usr/local/etc/nginx/nginx.conf
brew services restart nginx

以上诚寻路过的大神指教一下:

1、几乎每次开机非sudo命令执行都会重复出现

2、相同的测试文件路径下,nginx root 配置的路径生效,静态文件正常访问,动态php文件访问是下载源文件,但测试在 /usr/local/var/www 软链接到:/usr/local/Cellar/nginx/1.17.8/html 的php动态文件是正常的,具体显示为:

<?

c715f4516eb3ce10b6dfb13bd80ce9fd.png

但此test.php文件,并非在http://local.test:8088/ nginx server配置的路径下?

root   /Users/zaneli/WorkSpace/test;

感谢狂野小青年大牛的耐心指正,已正确解决:

解决方式:location ~ .php$ {     
           root   /Users/zaneli/WorkSpace/test;  # 划重点:这地方得再配置一下!
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值