Nginx for Mac

tags: Nginx Mac

安裝nginx

$ brew install nginx

啟動

$ nginx    //localhost:8080  可以看到表示正常啟動
$ nginx -v  //版本

停止

$ nginx -s stop

重啟

$ nginx -s reload

localhost:8080 的根目錄位置

usr > local > var > www 下

設定

設定檔案在 /usr/local/etc/nginx/nginx.conf 也可以在servers目錄裡面加入.conf檔

server配置

將leonsnoopyleo.com路徑透過proxy導入到 localhost:8877
leonsnoopyleo.com/leo/cgi-bin 導入到 localhost:8888/leo/cgi-bin

server {
    listen       80;
    server_name  leonsnoopyleo.com;
    
    location / {
      proxy_pass http://localhost:8877;
    }

    location /leo/cgi-bin {
    	proxy_pass http://localhost:8888/leo/cgi-bin;
    }
}

將localhost:8877 指向絕對路徑的資料夾開啟index.html

server {
    listen       8877;
    server_name  localhost;

    location / {
        root /Users/leo/Desktop/work/ifttt/dist/;
        index  index.html index.htm;
    }
}

如果要在本地使用網域取代localhost 可以在/etc/hosts文件下配置成這樣

原本的

127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

修改後

127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1 leonsnoopyleo.com

這樣dns就會優先判斷這個leonsnoopyleo.com網域指向到本機

開啟php

將nginx.conf 這個註解拿掉

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