首页 > 解决方案 > 在 mamp 免费版上使用 nginx 设置 vhost

问题描述

我想在 mamp 上设置虚拟主机,同时使用 nginx 而不是默认的 apache 服务器。

我浏览了堆栈溢出和网络,但我能找到的要么是纯 nginx(谈到sites-available默认情况下未在 mamp 上创建的文件夹),要么是关于 mamp 上带有 apache 的虚拟主机,或者是关于 mamp PRO 上带有 nginx 的虚拟主机。

我确实设法切换到 nginx,但我现在只能看到 403 错误(当我回到 apache 服务器时,我所有的虚拟主机都在工作)。我确实在我的主机文件中添加了这些行,但我无法让虚拟主机工作。这是我的MAMP/conf/nginx/nginx.conf

http {
    ...

    server {
        ...

        location / {
            index            index.html index.php;
        }

        location my-vhost-name.com {
            root             /Users/myName/Document/projectParentFolder/projectFolder;
            index            index.html index.php;
        }
    }
}

当我访问 my-vhost-name.com 时,我收到了来自 nginx 的 403 错误消息。

谢谢您的帮助。

标签: nginxmampvhosts

解决方案


  1. 在 MAMP/conf/nginx 目录中,我sites-enabled为各个站点创建了一个配置文件夹。

  2. 我为该站点添加了一个配置example.com文件MAMP/conf/nginx/sites-enabled

  3. 我将配置变量添加到MAMP/conf/nginx/sites-enabled/example.com

     server {
         charset utf-8;
         client_max_body_size 128M;
         sendfile off;
    
         listen 80;
    
         server_name example.com;
         root        /Applications/MAMP/htdocs/example.com/;
         index       index.php;
    
         location / {
             # Redirect everything that isn't a real file to index.php
             try_files $uri $uri/ /index.php$is_args$args;
         }
    
         location ~ \.php$ {
             try_files        $uri =404;
             fastcgi_pass     
             unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
             fastcgi_param    SCRIPT_FILENAME 
             $document_root$fastcgi_script_name;
             include          fastcgi_params;
         }
     }
    
  4. 在配置主文件的末尾(MAMP/conf/nginx/nginx.conf),我连接了sites-enabled文件夹中的所有配置:

        include sites-enabled/*;
     }
    
  5. 重启 MAMP


推荐阅读