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个配置结果将仍然一样。

 

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

 

posted @ 2016-10-02 03:44  blcblc  阅读(3243)  评论(0编辑  收藏  举报