【问题标题】:Nginx virtual block on Mac pointing to localhost's index.phpMac上的Nginx虚拟块指向localhost的index.php
【发布时间】:2020-06-16 19:51:23
【问题描述】:

我正在配置虚拟主机以在我的 Mac 上为 .localhost 域提供服务。但是当我在浏览器中打开“project.localhost”时,它显示的页面与浏览 localhost 的索引相同。 (ps:我还配置了 dnsmasq 和 /etc/hosts)。 (尝试服务 index.php)

这是我的 nginx.conf:

worker_processes  1;


events {
    worker_connections  1024; }


http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 64;

    client_max_body_size 50M;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        root   html;

        location / {
            # root   html;
            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;
        }

        location ~ \.php$ {
            # root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    include servers/*; }

这里是可用的网站:

    server {
        listen 80;
        listen project.localhost:80;
        server_name project.localhost;

        location / {
            root html/project.localhost/public_html;
            index index.php index.html index.htm;
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            root html/project.localhost/public_html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}

这里是 dnsmasq.conf:

address=/.localhost/127.0.0.1

这里是 /etc/hosts:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1   project.localhost www.project.localhost

但是当我在浏览器中浏览 project.localhost 时,它会显示与 localhost/index.php 相同的页面。我做错了什么?

【问题讨论】:

  • server_name 的值应该是 project.localhost 而不是 project.local
  • 我看不到 sites-available 中的文件是否已包含在您的 Nginx 配置中。您将包含来自servers 目录的文件(请参见最后一行)。使用nginx -T(大写T)查看所有包含文件的整个Nginx配置,并确保您的server块存在正确的server_name

标签: php macos nginx server


【解决方案1】:

正如@Richard 在评论部分提到的那样,我能够通过在我的 nginx.conf 中包含 sites-available/*.conf: include /usr/local/etc/nginx/sites-available/*.conf,就在 include server/* 之前解决这个问题。谢谢@Richard

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 2018-12-02
    • 2015-01-06
    • 2013-10-13
    • 2012-11-17
    • 1970-01-01
    • 2020-09-11
    • 2015-03-27
    • 2013-12-19
    相关资源
    最近更新 更多