【Nginx 1.20.1 基础】Nginx 在 MacOS 编译构建部署启动测试 2021_9_14

Nginx 在 MacOS 编译构建部署启动测试

引用

中级测试部分
1.nginx通过自定义header属性来转发不同的服务
构建部分
2.Nginx官网
3.Nignx构建官方文档
4.PCRE Download页面
5.Zlib官网 & Download页面
6.OpenSSL Download页面

命令

预先准备

  • nginx-1.20.1.tar.gz
  • pcre-8.45.tar.bz2
  • zlib-1.2.11.tar.gz
  • openssl-openssl-3.0.0.tar.gz

系统版本: macOS 11.4 (20F71)
内核版本: Darwin 20.5.0

执行

// 1.解压 Nginx && 依赖 【懒得写移动文件了自己随便拖进来】
$ mkdir nginx && cd nginx
$ tar xvf nginx-1.20.1.tar.gz

$ mkdir pcre && cd pcre
$ tar xvf pcre-8.45.tar.bz2

$ mkdir zlib && cd zlib
$ tar xvf zlib-1.2.11.tar.gz

$ mkdir openssl && cd openssl
$ tar xvf openssl-openssl-3.0.0.tar.gz

// 2.预配置
$ sudo mkdir /usr/local/nginx/nginx
$ cd nginx/nginx-1.20.1
$ sudo ./configure \
    --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre=/Users/inbreeze/Downloads/pcre/pcre-8.45 \
    --with-zlib=/Users/inbreeze/Downloads/zlib/zlib-1.2.11 \
    --with-openssl=/Users/inbreeze/Downloads/openssl/openssl-openssl-3.0.0

>>  ...
    Configuration summary
      + using PCRE library: /Users/inbreeze/Downloads/pcre/pcre-8.45
      + using OpenSSL library: /Users/inbreeze/Downloads/openssl/openssl-openssl-3.0.0/
      + using zlib library: /Users/inbreeze/Downloads/zlib/zlib-1.2.11

      nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/nginx/nginx"
      nginx modules path: "/usr/local/nginx/modules"
      nginx configuration prefix: "/usr/local/nginx"
      nginx configuration file: "/usr/local/nginx/nginx.conf"
      nginx pid file: "/usr/local/nginx/nginx.pid"
      nginx error log file: "/usr/local/nginx/logs/error.log"
      nginx http access log file: "/usr/local/nginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"


// 3.执行编译
$ sudo make install

>>  ...
    sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
    test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
    test -d '/usr/local/nginx' \
            || mkdir -p '/usr/local/nginx'
    test ! -f '/usr/local/nginx/nginx' \
            || mv '/usr/local/nginx/nginx' \
                '/usr/local/nginx/nginx.old'
    cp objs/nginx '/usr/local/nginx/nginx'
    test -d '/usr/local/nginx' \
            || mkdir -p '/usr/local/nginx'
    cp conf/koi-win '/usr/local/nginx'
    cp conf/koi-utf '/usr/local/nginx'
    cp conf/win-utf '/usr/local/nginx'
    test -f '/usr/local/nginx/mime.types' \
            || cp conf/mime.types '/usr/local/nginx'
    cp conf/mime.types '/usr/local/nginx/mime.types.default'
    test -f '/usr/local/nginx/fastcgi_params' \
            || cp conf/fastcgi_params '/usr/local/nginx'
    cp conf/fastcgi_params \
            '/usr/local/nginx/fastcgi_params.default'
    test -f '/usr/local/nginx/fastcgi.conf' \
            || cp conf/fastcgi.conf '/usr/local/nginx'
    cp conf/fastcgi.conf '/usr/local/nginx/fastcgi.conf.default'
    test -f '/usr/local/nginx/uwsgi_params' \
            || cp conf/uwsgi_params '/usr/local/nginx'
    cp conf/uwsgi_params \
            '/usr/local/nginx/uwsgi_params.default'
    test -f '/usr/local/nginx/scgi_params' \
            || cp conf/scgi_params '/usr/local/nginx'
    cp conf/scgi_params \
            '/usr/local/nginx/scgi_params.default'
    test -f '/usr/local/nginx/nginx.conf' \
            || cp conf/nginx.conf '/usr/local/nginx/nginx.conf'
    cp conf/nginx.conf '/usr/local/nginx/nginx.conf.default'
    test -d '/usr/local/nginx' \
            || mkdir -p '/usr/local/nginx'
    test -d '/usr/local/nginx/logs' \
            || mkdir -p '/usr/local/nginx/logs'
    test -d '/usr/local/nginx/html' \
            || cp -R html '/usr/local/nginx'
    test -d '/usr/local/nginx/logs' \
            || mkdir -p '/usr/local/nginx/logs'

// 4.测试启动 [注意: /usr/local/nginx/nginx/ 为二进制目录 预配置时会提示, 建议目录名改为 sbin or bin 更符合惯例]
$ sudo /usr/local/nginx/nginx/nginx
$ curl localhost
>>  <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>

    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>

    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>

中级测试

sudo mkdir /etc/nginx
sudo vim nginx.conf
>>>
...
http {
    ...
    include /etc/nginx/*.conf;
    ...
}
...
<<<

sudo vim /etc/nginx/wx_test.conf
>>>
upstream wx {
    server 127.0.0.1:8080;
}

server {
    listen 8008;
    server_name localhost;

    root html;
    index index.html;

    charset utf-8;
    underscores_in_headers on;
    location / {
        #测试 header 转发
        if ($http_is_user = "true") {
            proxy_pass http://wx;
        }
    }
}
<<<

sudo vim /etc/nginx/wx_home.conf
>>>
server {
    listen 8080;
    server_name localhost;

    root /var/www/html;
    index wx.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
<<<

sudo mkdir -p /var/www/html
sudo vim /var/www/html/wx.html
>>>
<h1>这里是由用户触发跳转的wx页面</h1>
<<<

sudo nginx/nginx -s reload

curl --location --request GET 'localhost:8008' \
--header 'is_user: true'
<h1>这里是由用户触发跳转的wx页面~</h1>
上一篇:深度学习1:Regression-CaseStudy(预测宝可梦进化后CP值)


下一篇:java 在jar包外调整 log4j/logback日志级别