目录

  • 一. 问题
  • 二. 安装
  • 三. 配置
  • 3.1 nginx.conf
  • 3.2 代理服务器配置
  • 3.3 域名映射
  • 3.4 SSL/TLS 配置支持
  • 3.4.1 自签名创建密钥文件和证书
  • 3.4.2 nginx 配置与开启 SSL/TLS 支持
  • 3.5 配置优化 案例
  • 3.5.1 nginx.conf 案例
  • 3.5.2 SSL/TLS 配置参数化支持 案例
  • 3.5.3 servers_xxx.conf 案例
  • 3.5.4 servers_ftp.conf 案例
  • 四. 启动与关闭
  • 五. 参考

一. 问题

  最近弄了些东西,想放服务器上,次哦,远程存储库不存在,算了,自己本地先搞个瞅瞅;
  这就很尴尬了,电脑上没有。

二. 安装

首先输入如下命令,查找 nginx 一下,看有没有稳定版本,

$ brew search nginx

==> Formulae
nginx ✔

发现只有一个选项,没得选,然后进行安装,命令如下:

$ brew install nginx

安装过程如下:

...
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2021-10-26
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:1bbd45c16a0b9912174c553a6d7ae1b67b11abbeb3155eaf03109bb62d8e5381
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:1bbd45c16a0b9912174c553a6d7ae1b67b11abbeb3155eaf03109bb62d8e5381?se=2021-12-
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1l_1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/blobs/sha256:9a14367813591b51e30828c7d86499479bc6201954f6b10ed591b40cd3b71cc1
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:9a14367813591b51e30828c7d86499479bc6201954f6b10ed591b40cd3b71cc1?se=2021-12-
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pcre/manifests/8.45
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pcre/blobs/sha256:5e5cc7a5bf8bb6488ec57d4263bf6b0bc89e93252a0a2460f846de29373162d8
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:5e5cc7a5bf8bb6488ec57d4263bf6b0bc89e93252a0a2460f846de29373162d8?se=2021-12-
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.21.4
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:1705176bc483a5fe2dfaa0872a370f6b7d05f2e3283a49c444276ad72673a71e
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:1705176bc483a5fe2dfaa0872a370f6b7d05f2e3283a49c444276ad72673a71e?se=2021-12-
######################################################################## 100.0%
==> Installing nginx 1.21.4 

==> Installing dependencies for nginx: ca-certificates, openssl@1.1 and pcre
==> Installing nginx dependency: ca-certificates
==> Pouring ca-certificates--2021-10-26.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /usr/local/Cellar/ca-certificates/2021-10-26: 3 files, 208.5KB
==> Installing nginx dependency: openssl@1.1
==> Pouring openssl@1.1--1.1.1l_1.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/openssl@1.1/1.1.1l_1: 8,073 files, 18.5MB
==> Installing nginx dependency: pcre
==> Pouring pcre--8.45.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.45: 204 files, 5.7MB
==> Installing nginx
==> Pouring nginx--1.21.4.monterey.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To restart nginx:
  brew services restart nginx
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/nginx/bin/nginx -g daemon off;
==> Summary
🍺  /usr/local/Cellar/nginx/1.21.4: 26 files, 2.2MB

三. 配置

3.1 nginx.conf

默认配置路径:

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

修改配置如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    # 日志格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    # log_format  main  '$status $body_bytes_sent "$http_referer" ';
    # log_format  main  '"$http_user_agent" "$http_x_forwarded_for"';

    # 日志: logs/access.log
    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        location / {
            # 配置代理服务器 www.xxx.com
            proxy_pass http://www.xxx.com:8081;
        }

        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

上述修改只需要关注以下几点:

  1. 日志格式和日志目录,比葫芦画瓢,按照官方文档默认放开选项即可;
  2. 代理服务器的配置,重点关注:proxy_pass http://www.xxx.com:8081; 这句配置项;

3.2 代理服务器配置

由于上述配置项中已经有如下配置项:

...
include servers/*;
...

所以,我们只需再给定的配置目录下新建一个 servers 目录,即如下路径所示:

- nginx.conf
- nginx.conf.default
+ servers
    -  proxy_nginx_1.conf

然后再 servers 目录下,新建一个 proxy_nginx_1.conf 配置文件:

server {
    # 服务器名称和别名
    server_name  proxy_nginx_1  alias  www.xxx.com;
    # 端口号
    listen       8081;
    # 站点 WebSite 目录的绝对路径
    root /Users/xxx/.../www/website;

    location / {
        index  index.html index.htm;
    }
}

上述配置可以有多个虚拟主机和代理服务器;

后续补充,此类相关项…

3.3 域名映射

如果本地访问需要配置指定域名映射,配置文件路径为:

/etc/hostes

然后添加如下信息:

127.0.0.1        localhost
# 设置的服务器域名或别名
127.0.0.1        www.xxx.com
127.0.0.1        ftp.xxx.com

然后执行(选择合适的系统选项)如下命令,使得 DNS 生效:

windows env: `ipconfig /flushdns`
linux env:   `systemctl restart nscd`
mac env:     `dscacheutil -flushcache` or `sudo dscacheutil -flushcache` or
          `sudo killall -HUP mDNSResponder`

比如,在 Mac 环境下,就执行:

$ dscacheutil -flushcache

3.4 SSL/TLS 配置支持

此处我们只讨论,自签名创建密钥文件和证书的情况;

3.4.1 自签名创建密钥文件和证书
  1. 首先再一个干净的目录下,新建一个 req.cnf 文件(推荐),命令如下:
$ touch req.cnf

输入如下信息:

[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[req_distinguished_name]
C = CN
ST = BeiJing
L = BeiJing
O = xxx All Rights Reserved Inc.
OU = xxx
CN = www.xxx.com
[v3_req]
keyUsage = critical, digitalSignature, keyAgreement
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = www.xxx.com
DNS.2 = xxx.com
DNS.3 = xxx.net
  1. 然后执行如下命令生成:
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./nginx-selfsigned.key -out ./nginx-selfsigned.crt -config req.cnf -sha256

Generating a 2048 bit RSA private key
....+++
............+++
writing new private key to './nginx-selfsigned.key'
-----
  1. 在我们使用OpenSSL 的同时,我们还应该创建一个完整的Diffie-Hellman 组,用于与客户协商完整的保密。我们可以通过输入以下内容来执行:
$ openssl dhparam -out ./nginx-selfsigned-dhparam.pem 2048

Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
...........................................+...............................................................+.......+.........................................................+.........................+..............+..............................+................++*++*

此时当前目录下,会有 4 个文件,如下所示:

- ./req.cnf
- ./nginx-selfsigned-dhparam.pem
- ./nginx-selfsigned.crt
- ./nginx-selfsigned.key
  1. 也可以采用如下问答方式, 来生成(不推荐):
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./nginx-selfsigned.key -out ./nginx-selfsigned.crt

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:CN
State or Province Name (full name) []:BeiJing 
Locality Name (eg, city) []:BeiJing
Organization Name (eg, company) []:xxx All Rights Reserved Inc.
Organizational Unit Name (eg, section) []:xxx
Common Name (eg, fully qualified host name) []:www.xxx.com
Email Address []:xxx@qq.com

Generating a 2048 bit RSA private key
.............................................................................................+++
....+++
writing new private key to './nginx-selfsigned.key'
-----
  1. 此时,我们需要把生成的自签名创建密钥文件和证书,放到如下位置:

当然,这个自签名创建密钥文件和证书,可以放自定义目录,也可以放系统目录/etc/ssl/ 下, 推荐如下目录:

$ /usr/local/etc/nginx/ssl/certs/nginx-selfsigned-dhparam.pem
$ /usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt
$ /usr/local/etc/nginx/ssl/private/nginx-selfsigned.key
3.4.2 nginx 配置与开启 SSL/TLS 支持
  1. servers 目录下的 proxy_nginx_1.conf 中,写入如下细节配置项:
server {
    # from https://cipherli.st/ and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
    # listen 8081 default_server;
    # listen [::]:8081 default_server;
    # server_name server_domain_or_IP;
    # return 302 https://$server_name$request_uri;

    # 端口号
    # 关闭此处 8081 讲不支持 http 方式访问,开启将同时支持 http/https 方式, 同时访问
    # listen       8081;
    listen       8081 default backlog=2048;
    # 端口号
    listen       443 ssl;
    # 服务器名称和别名
    server_name  proxy_nginx_1  alias  www.xxx.com;
    client_max_body_size 1024M;
    keepalive_timeout   70;

    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers  on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    # ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
    # ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_ecdh_curve secp384r1;
    # 1. One megabyte of the cache contains about 4000 sessions.
    # 2. The default cache timeout is 5 minutes.
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;
    # ssl_trusted_certificate /usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
    # Disable preloading HSTS for now.  You can use the commented out header line that includes
    # the "preload" directive if you understand the implications.
    # add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    ssl_certificate /usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /usr/local/etc/nginx/ssl/private/nginx-selfsigned.key;
    ssl_dhparam /usr/local/etc/nginx/ssl/certs/nginx-selfsigned-dhparam.pem;
    # ssl_certificate     www.xxx.com.crt;
    # ssl_certificate_key www.xxx.com.key;

    # 站点 WebSite 目录的绝对路径
    root /Users/xxx/.../www/website;

    location / {
        index  index.html index.htm;
    }
}

上述配置,可以修改 xxx 地方和自定义命名;

  1. 然后, 使用如下命令配置,检查配置文件是否配置通过:
$ nginx -t

nginx: [warn] "ssl_stapling" ignored, issuer certificate not found for certificate "/usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt"
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

nginx: [warn] “ssl_stapling” ignored 无需考虑,因为是自签名;

  1. 然后, 使用如下命令配置,使得配置文件生效:
$ nginx -s reload
nginx: [warn] "ssl_stapling" ignored, issuer certificate not found for certificate "/usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt"

nginx: [warn] “ssl_stapling” ignored 无需考虑,因为是自签名;

  1. 如果上述步骤,都通过的话,此时只需重启 nginx 服务器,即可:
$ brew services restart nginx

Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
  1. 此时浏览器中输入配置的指定域名映射网址:

此时都应该加载成功, 如果浏览器弹出不安全, 点击继续前往即可;

3.5 配置优化 案例

经过前面配置操作,现给出一般静态站点 + FTP 站点浏览 + SSL/TLS 访问支持的一般配置项, 优化如下:

  1. nginx.conf 配置项中啥也不做(反向代理操作情况例外), 只配置全局主机性质的配置项, 在 servers/* 下添加代理或主机配置文件;
  2. SSL/TLS 配置项参数化配置,服务于所有代理或主机配置文件, 使用 include servers/servers_ssl_param.conf; 指令;
  3. servers_xxx.conf 面向一般的静态网站配置, 如需提供 SSL/TLS, 直接使用 include servers/servers_ssl_param.conf; 指令【推荐】;
  4. servers_ftp.conf 面向一般的文件浏览服务, 如需提供 SSL/TLS, 直接使用 include servers/servers_ssl_param.conf; 指令【推荐】;

配置路径如下:

nginx 指令的根目录为: /usr/local/etc/nginx/

- nginx.conf
- nginx.conf.default
+ servers
    -  servers_xxx.conf
    -  servers_ftp.conf
    -  servers_ssl_param.conf

Notice: 使得案例生效, 别忘了检查配置项, 重载配置文件, 重启服务器;

  1. 执行配置项检查, 命令如下:
$ nginx -t
  1. 执行重载配置文件, 命令如下:
$ nginx -s reload
  1. 执行重启服务器, 命令如下:
$ brew services restart nginx
3.5.1 nginx.conf 案例
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    # log_format  main  '$status $body_bytes_sent "$http_referer" ';
    # log_format  main  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    include servers/*;
}
3.5.2 SSL/TLS 配置参数化支持 案例

Notice: 此处配置为参数配置,不是代理或主机配置项!!!

# from https://cipherli.st/ and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

# listen       8081 default backlog=2048;
# listen       443 ssl;
# client_max_body_size 1024M;
# client_max_body_size 1G;
# keepalive_timeout   70;

ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers  on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
# ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
# ssl_ciphers         HIGH:!aNULL:!MD5;
ssl_ecdh_curve secp384r1;
# 1. One megabyte of the cache contains about 4000 sessions.
# 2. The default cache timeout is 5 minutes.
ssl_session_cache   shared:SSL:10m;
ssl_session_timeout 5m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# ssl_trusted_certificate /usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Disable preloading HSTS for now.  You can use the commented out header line that includes
# the "preload" directive if you understand the implications.
# add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
ssl_certificate /usr/local/etc/nginx/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /usr/local/etc/nginx/ssl/private/nginx-selfsigned.key;
ssl_dhparam /usr/local/etc/nginx/ssl/certs/nginx-selfsigned-dhparam.pem;
# ssl_certificate     www.xxx.com.crt;
# ssl_certificate_key www.xxx.com.key;
3.5.3 servers_xxx.conf 案例

面向一般静态站点的配置项:

server {
    # HTTP 支持, 【不推荐】
    # listen       *:8081;

    # SSL/TLS 配置支持
    listen       *:443 ssl;
    include servers/servers_ssl_param.conf;

    server_name  www.xxx.com;

    client_max_body_size 1024M;

    error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # 站点 WebSite 目录的绝对路径
    root /Users/xxx/.../www/website;

    location / {
        index  index.html index.htm;
    }

    location /public {
        proxy_pass https://ftp.xxx.com;
    }
}

此时浏览器中输入如下地址,应该能正确展示:

# HTTP 支持, 【不推荐】
# listen       *:8081;
- http://www.xxx.com/

# SSL/TLS 配置支持
# listen       *:443 ssl;
# include servers/servers_ssl_param.conf;
- https://www.xxx.com/
3.5.4 servers_ftp.conf 案例

面向一般文件浏览服务的配置项:

server {
    # http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
    # HTTP 支持, 【不推荐】
    # listen       *:8082;

    # SSL/TLS 配置支持
    listen       *:443 ssl;
    include servers/servers_ssl_param.conf;

    server_name  ftp.xxx.com;
    # 中文支持
    charset utf-8;

    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    sendfile on;

    location  /downloads {
        # FTP 目录的绝对路径, 如若采用 alias 必须以`/` 结尾
        alias /Users/xxx/.../Downloads/;

		if ($request_filename ~* ^.*?.(txt|rtf|csv|log|json|doc|xls|ppt|pages|numbers|keynote|pdf|epub|rar|gz|zip|chm|docx|ps1|bat|sh|apk|ipa|exe|dmg|pkg|jar|xlsx|ppt|pptx|jpg|jpeg|gif|png|webp|psb|mp4)$){
           add_header Content-Disposition: attachment;
        }
    }

    location /public {
        # FTP 目录的绝对路径, 如若采用 alias 必须以`/` 结尾
        alias /Users/xxx/.../Public/Temp/;

		if ($request_filename ~* ^.*?.(txt|rtf|csv|log|json|doc|xls|ppt|pages|numbers|keynote|pdf|epub|rar|gz|zip|chm|docx|ps1|bat|sh|apk|ipa|exe|dmg|pkg|jar|xlsx|ppt|pptx|jpg|jpeg|gif|png|webp|psb|mp4)$){
           add_header Content-Disposition: attachment;
        }
    }

    location / {
        # FTP 目录的绝对路径
        root /Users/xxx/.../Shared/;

		if ($request_filename ~* ^.*?.(txt|rtf|csv|log|json|doc|xls|ppt|pages|numbers|keynote|pdf|epub|rar|gz|zip|chm|docx|ps1|bat|sh|apk|ipa|exe|dmg|pkg|jar|xlsx|ppt|pptx|jpg|jpeg|gif|png|webp|psb|mp4)$){
           add_header Content-Disposition: attachment;
        }
    }

    error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

此时浏览器中输入如下地址,应该能正确展示:

# HTTP 支持, 【不推荐】
# listen       *:8082;
- http://ftp.xxx.com/
- http://ftp.xxx.com/downloads/
- http://ftp.xxx.com/public/

# SSL/TLS 配置支持
# listen       *:443 ssl;
# include servers/servers_ssl_param.conf;
- https://ftp.xxx.com/
- https://ftp.xxx.com/downloads/
- https://ftp.xxx.com/public/

四. 启动与关闭

使用如下命令,查看启动与关闭nginx 命令:

$ brew services -h

Usage: brew services [subcommand]

Manage background services with macOS' launchctl(1) daemon manager.

If sudo is passed, operate on /Library/LaunchDaemons (started at boot).
Otherwise, operate on ~/Library/LaunchAgents (started at login).

[sudo] brew services [list]:
    List all managed services for the current user (or root).

[sudo] brew services info (formula|--all):
    List all managed services for the current user (or root).

[sudo] brew services run (formula|--all):
    Run the service formula without registering to launch at login (or boot).

[sudo] brew services start (formula|--all):
    Start the service formula immediately and register it to launch at login
(or boot).

[sudo] brew services stop (formula|--all):
    Stop the service formula immediately and unregister it from launching at
login (or boot).

[sudo] brew services restart (formula|--all):
    Stop (if necessary) and start the service formula immediately and register
it to launch at login (or boot).

[sudo] brew services cleanup:
    Remove all unused services.
  1. 启动 nginx 命令:
$ brew services start nginx
  1. 启动非后台运行 nginx 命令:
$ /usr/local/opt/nginx/bin/nginx -g daemon off;
  1. 重启 nginx 命令:
$ brew services restart nginx
  1. 关闭 nginx 命令:
$ brew services stop nginx
  1. 显示 nginx 版本信息及配置信息:
$ nginx -V
  1. 显示 nginx 配置路径命令:
$ nginx -t
  1. nginx 配置生效命令:
$ nginx -s reload

五. 参考

  1. http://nginx.org/en/docs/
  2. https://www.ietf.org/rfc/rfc5280.txt
  3. http://nginx.org/en/docs/http/ngx_http_autoindex_module.html