Nginx安裝及配置文件解釋


安裝nginx,還是在mac上面用brew比較方便。

首先,brew install nginx,提示改權限

sudo chown -R $(whoami) /usr/local

 

然后brew install nginx,下載一些包始終太慢。

然后查看 brew --cache,發現緩存在 

/Users/baidu/Library/Caches/Homebrew

在下載慢的包,瀏覽器下載好放在這個目錄,然后再次 brew install nginx 成功。

 

啟動命令:brew services start nginx

然后http://127.0.0.1:8080/ 可以訪問:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

 

查看版本:

$ nginx -v
nginx version: nginx/1.10.1

 

默認的nginx配置文件是在

/usr/local/etc/nginx/nginx.conf

root目錄是在 

/usr/local/Cellar/nginx/1.10.1/html

 

本身Nginx啟動過程中的日志是在:

/usr/local/var/log/nginx/error.log
access.log

在Nginx的配置文件中打開日志后:

error_log  logs/error.log;
對應的是:

/usr/local/Cellar/nginx/1.10.1/logs/

 

 Mac默認的Php和php-fpm安裝了:

$ which php
/usr/bin/php
$ which php-fpm
/usr/sbin/php-fpm
$ ls /etc/php
php-fpm.conf.default  php.ini.default      
$ php -v
PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45)

 

啟動php-fpm前要准備好配置文件:

sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

然后 php-fpm , 仍然報下面的錯
$ php-fpm
[04-Oct-2016 16:46:40] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)

需要
sudo vi /private/etc/php-fpm.conf
改兩個地方:
error_log = /usr/local/var/log/php-fpm.log
pid = /usr/local/var/run/php-fpm.pid

然后php-fpm
php示例文件
<?php
echo "hi";
?>

然后 php -f hi.php
可以運行

 

然后我嘗試把端口設成80,但是在這個error log:

/usr/local/var/log/nginx/error.log 報錯:

2016/10/04 17:00:12 [emerg] 26905#0: bind() to 0.0.0.0:80 failed (13: Permission denied)

所以只好改成7080

 

然后發現php文件始終是在瀏覽器頁面報這個錯:

"File not found",然后在后台日志報這個錯:

2016/10/04 17:01:39 [error] 27052#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1:7080"

在網上搜索之后,需要把/usr/local/etc/nginx/nginx.conf 里面 fastcgi_param里面的 /scripts$fastcgi_script_name; 改成 $document_root$fastcgi_script_name; 重啟之后即可。

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;
        }

 

在/usr/local/Cellar/nginx/1.10.1/html放一個index.php,內寫:

<?php
echo "Here is php index";
phpinfo(); ?>

然后訪問:http://127.0.0.1:7080/index.php 成功顯示:Here is php index

 

————————————————————————————

以下為配置文件:

location ~ 區分大小寫 ~* 不區分大小寫

 

這篇文章可以看看:

http://www.cnblogs.com/top5/archive/2011/03/04/1970633.html

例子:

location = / {
# 只匹配 / 查詢。
[ configuration A ]
}

location / {
# 匹配任何查詢,因為所有請求都已 / 開頭。但是正則表達式規則和長的塊規則將被優先和查詢匹配。
[ configuration B ]
}

location ^~ /images/ {
# 匹配任何已 /images/ 開頭的任何查詢並且停止搜索。任何正則表達式將不會被測試。
[ configuration C ]
}

location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何已 gif、jpg 或 jpeg 結尾的請求。然而所有 /images/ 目錄的請求將使用 Configuration C。
[ configuration D ]
}

例子請求:

/ -> configuration A

/documents/document.html -> configuration B

/images/1.gif -> configuration C

/documents/1.jpg -> configuration D

注意:按任意順序定義這4個配置結果將仍然一樣。

 

----------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM