Mac OS X环境下Nginx搭建多本地开发站点故障排查求助
本地多开发站点无法访问问题排查方案
我在本地搭建多个开发用站点,已配置hosts文件,但无法访问这些站点。在浏览器地址栏输入abcd_rev0.dvp时,浏览器会跳转到谷歌搜索页面,且站点日志目录无数据,地址与站点未建立关联。相关配置文件及浏览器访问截图如下:
一、相关配置文件
1. Hosts文件
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost #127.0.0.1 localhost nginx.conf 127.0.0.1 localhost abcd_rev0.dvp 127.0.0.1 localhost abcd_rev1.dvp #127.0.0.1 dk_analytics_rev0.dvp #127.0.0.1 dk_analytics_rev1.dvp 255.255.255.255 broadcasthost ::1 localhost
2. Nginx主配置文件(注意当前文件名为nains.conf,应为nginx.conf)
user UserId staff; worker_processes 1; pid /usr/local/etc/nginx/logs/nginx.pid; include etc/nginx/modules-enables/*.conf; error_log /usr/local/etc/nginx/logs/error.log; # error_log /usr/local/etc/nginx/logs/error.log notice; # error_log /usr/local/etc/nginx/logs/error.log info; events { worker_connections 1024; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name#in_redirect off; include /usr/local/etc/nginx/mime.types; default_type application/octet-stream; ## # Buffer Settings ## client_body_buffer_size 10k; client_max_body_size 8m; client_header_buffer_size 1k; client_body_timeout 10s; client_header_timeout 10s; send_timeout 1s; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #TLSv2 TLSv3; # Dropping SSLv3, ref: PODDLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /usr/local/etc/nginx/logs/access.log; error_log /usr/local/etc/nginx/logs/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; gzip_comp_level 3; # gzip_buffers 16 8k; # gzip_http_version 1.1; gzip_types text/plain; gzip_types text/css; gzip_types application/json; gzip_types text/xml; gzip_types application/xml; gzip_types application/xml+rss; gzip_types text/javascript; ## # Virtual Host Configs ## # include /etc/nginx/conf.d/*.congf; # include /etc/nginx/sites-enabled/*; # Load all configuration files in sites-enabled include sites-enabled/*.conf; }
3. 站点1配置文件
server { listen 80; listen [::]:80; server_name abcd_rev0.dvp; root /Users/UserId/Sites/Rev0; index index.php index.html index.htm; access_log /Users/UserId/Sites/Rev0/logs/access.log; error_log /Users/UserId/Sites/Rev0/logs/error.log; location / { proxy_pass http://localhost:4567/; } location /images { autoindex on; } location ~ \.php { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_index index.php; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /Users/UserId/Sites/Rev0/; index error.html; } }
4. 站点2配置文件
server { listen 80; listen [::]:80; server_name abcd_rev0.dvp; root /Users/UserId/Sites/Rev1; index index.php index.html index.htm; access_log /Users/UserId/Sites/Rev1/logs/access.log; error_log /Users/UserId/Sites/Rev1/logs/error.log; location / { proxy_pass http://localhost:4568/; } location /images { autoindex on; } location ~ \.php { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_index index.php; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /Users/UserId/Sites/Rev1/; index error.html; } }
浏览器访问截图

二、问题排查与修复方案
1. 浏览器地址解析问题
- 直接输入
abcd_rev0.dvp时,浏览器会默认当作搜索关键词,必须加上协议头http://,即访问http://abcd_rev0.dvp。 - 验证hosts解析是否生效:执行命令
ping abcd_rev0.dvp,确认返回127.0.0.1。
2. Hosts文件优化(可选)
将hosts中站点域名配置简化为单独一行,避免混淆:
127.0.0.1 abcd_rev0.dvp 127.0.0.1 abcd_rev1.dvp
3. Nginx主配置文件修复
- 文件名修正:将
nains.conf重命名为nginx.conf,这是Nginx默认加载的主配置文件名。 - 模块路径修正:将
include etc/nginx/modules-enables/*.conf;改为绝对路径并修正拼写错误:include /usr/local/etc/nginx/modules-enabled/*.conf; - 站点配置路径修正:将
include sites-enabled/*.conf;改为绝对路径,确保Nginx能找到站点配置:include /usr/local/etc/nginx/sites-enabled/*.conf;
4. 站点配置文件修复
- 站点2的server_name修正:将站点2配置中的
server_name abcd_rev0.dvp;改为server_name abcd_rev1.dvp;,否则两个站点共用同一个域名,Nginx只会匹配第一个server块。
5. Nginx配置生效验证
- 执行
nginx -t检查配置语法是否正确,无报错再执行nginx -s reload重启Nginx,让配置生效。
6. 后端服务状态检查
- 确认站点1代理的
localhost:4567和站点2代理的localhost:4568服务已经启动。 - 确认PHP-FPM服务(监听9000端口)处于运行状态,否则PHP文件无法解析。
内容的提问来源于stack exchange,提问作者EdouardDKP
相关产品推荐
相关产品推荐

