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](https://i-blog.csdnimg.cn/blog_migrate/19371672de6b015542456c0c2a367277.jpeg)
二、具体问题及时下解决方式
1、每次开机后,执行 nginx -t,出现
![dbf12153e85eed80186d0e6dcd21af9f.png](https://i-blog.csdnimg.cn/blog_migrate/6c1844afb63ca32242f3a5cb7313b254.jpeg)
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](https://i-blog.csdnimg.cn/blog_migrate/9cb47dec532dcd4e37096cdc5e014083.jpeg)
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](https://i-blog.csdnimg.cn/blog_migrate/02d09df15c4bcf5cf7fec1c3839985b6.jpeg)
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](https://i-blog.csdnimg.cn/blog_migrate/b47677cf36e2c793cee453f6ae4ce063.jpeg)
但此test.php文件,并非在http://local.test:8088/ nginx server配置的路径下??
root /Users/zaneli/WorkSpace/test;
感谢狂野小青年大牛的耐心指正,已正确解决:
解决方式:location ~ .php$ {
root /Users/zaneli/WorkSpace/test; # 划重点:这地方得再配置一下!
}