导航
参考资料:https://legacy.gitbook.com/book/hfpp2012/nginx/details
何为 nginx
nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP proxy server, originally written by Igor Sysoev.
nginx是一个 HTTP 服务器,也是一个反向代理服务器。
Mac 下安装 nginx
(其他安装方式略)
brew install nginx
安装完成后,用 nginx -V 查看详细安装信息
常用命令
nginx,启动服务器nginx -s reload,重新加载配置nginx -s quit,退出
参考官方文档 http://nginx.org/en/docs/beginners_guide.html
里程碑1:启动 nginx
命令行执行 nginx 即可,默认端口 8080,浏览器访问截图如下:
nginx 配置文件
查看配置文件:vi /usr/local/etc/nginx/nginx.conf
核心关注点:
http {
    # 虚拟服务器配置
    server {
        # 端口号
        listen       8080;
        # 域名
        server_name  localhost;
        # 域名后的相对路径
        location / {
            # 指定根目录
            root   html;  # 即 /usr/local/Cellar/nginx/1.13.12/html
            # 定义此路径下默认访问的文件名
            index  index.html index.htm;
        }
    }
    # 引入其他配置文件
    include servers/*;
}
我们可以在 servers 目录下增加自定义配置文件。
里程碑2:访问静态页面
新建测试页面 index.html
mkdir -p ~/nginxwww/test
vi ~/nginxwww/test/index.html
内容如下:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <h1>Hello Man!</h1>
    </body>
</html>
新建配置文件 test.conf
cd /usr/local/etc/nginx/servers
vi test.conf
内容如下:
# 虚拟主机配置
server {
    # 监听端口
    listen       8012;
    # 域名设定,可以有多个
    server_name  localhost;
    root /Users/plough/nginxwww/test/; # 该项要修改为你准备存放相关网页的路径
    location / {
        # 定义路径下默认访问的文件名
        index index.html;
        # 打开目录浏览功能,可以列出整个目录
        autoindex on;
    }
}
重载 nginx 配置
nginx -s reload
浏览器访问
里程碑3:访问 PHP 页面
新建测试页面
vi ~/nginxwww/test/index.php
内容为
<?php echo phpinfo();?>
安装 PHP
brew install php
安装过程中遇到些问题,逐一百度解决。
启动 PHP
执行 php-fpm 命令,在 localhost:9000 上开启一个 socket 服务。
修改nginx配置文件test.conf
# 虚拟主机配置
server {
    # 监听端口
    listen       8012;
    # 域名设定,可以有多个
    server_name  localhost;
    root /Users/plough/nginxwww/test/; # 该项要修改为你准备存放相关网页的路径
    location / {
        # 定义路径下默认访问的文件名
        index index.php;
        # 打开目录浏览功能,可以列出整个目录
        autoindex on;
    }
    # proxy the php scripts to php-fpm
    location ~ \.php$ {
        # fastcgi配置
        include /usr/local/etc/nginx/fastcgi.conf;
        # 指定是否传递4xx和5xx错误信息到客户端
        fastcgi_intercept_errors on;
        # 指定FastCGI服务器监听端口与地址,可以是本机或者其它
        fastcgi_pass   127.0.0.1:9000;
    }
}
重载 nginx 配置
nginx -s reload



