美文网首页
mac下nginx的安装、使用、配置SSL

mac下nginx的安装、使用、配置SSL

作者: Bbang呀_ | 来源:发表于2019-12-17 17:10 被阅读0次

    安装

    没安装homebrew的话先安装homebrew:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  
    

    安装nginx:

    brew install nginx
    

    nginx使用命令

    启动:
    nginx

    为了获取所有正在运行的nginx进程列表,可以使用ps命令,例如:
    ps -ax | grep nginx

    如果执行的结果是

      521 ??         0:00.02 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;  
     1562 ??         0:00.14 nginx: worker process  
    89010 ttys000    0:00.00 grep nginx
    

    表示已启动成功
    访问localhost:8080,看到nginx的页面,说明成功安装和启动好了。

    一旦nginx启动以后,就可以通过 -s 参数来控制它。


    image.png

    nginx启动状态下,修改nginx.conf后:
    测试配置语法是否正确:
    nginx -t
    重新加载配置,执行下面的命令:
    nginx -s reload

    nginx配置在/usr/local/etc/nginx/nginx.conf中
    默认配置:

        server {
            listen       8080; // 监听8080端口
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm; // 默认目录是/usr/local/var/www,因此启动nginx访问8080端口访问的是/usr/local/var/www下的index.html文件
            }
    
            #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;
            }
    

    查日志

    cd /usr/local/var/log/nginx
    tail -f access.log
    tail -f error.log

    配置SSL

    1、找到Nginx文件夹
    cd /usr/local/etc/nginx

    2、openssl生成自签名证书

    创建服务器私钥,命令会让你输入一个口令

    openssl genrsa -out server.key 1024
    

    根据私钥生成证书申请,创建签名请求的证书(CSR)

    openssl req -new -key server.key -out server.csr
    

    下面的选项至少写一个,才可以生成证书成功

    Country Name (2 letter code) []:ch
    State or Province Name (full name) []:
    Locality Name (eg, city) []:
    Organization Name (eg, company) []:
    Organizational Unit Name (eg, section) []:
    Common Name (eg, fully qualified host name) []:
    Email Address []:
    

    在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

    $ cp server.key server.key.org
    
    $ openssl rsa -in server.key.org -out server.key
    

    最后标记证书使用上述私钥和CSR

    openssl x509 -req -in server.csr -out server.crt -signkey server.key -days 3650
    

    3、配置nginx:修改/usr/local/etc/nginx/nginx.conf 文件
    我们这里是为启动的本地工程配置ssl

        server {
            server_name xx.xx.xx; // 这里填要访问的域名
            listen 80;
            listen 443 ssl;
            ssl_certificate server.crt;
            ssl_certificate_key server.key;
            proxy_set_header X-Forwarded-Proto $scheme;
    
            location / {
                proxy_set_header host $host; // 把原http请求的Header中的Host字段也放到转发的请求里。
    如果不加这一行的话,nginx转发的请求header里就不会有Host字段,而服务器是靠这个Host值来区分你请求的是哪个域名的资源的。
                proxy_pass http://127.0.0.1:8082/; // 匹配到的路由请求转发到本地启动工程的ip和端口上
            }
        }
    

    卸载nginx

    brew uninstall nginx
    

    此时目录服务器上还留有nginx相关的文件,我们需要手动删除www、etc目录:
    /usr/local/etc/nginx 目录
    /usr/local/var/www 注意目录下你自己的源代码是否还在上面


    参考:
    https://www.jianshu.com/p/fc1e81efc867
    https://blog.csdn.net/cangck_x/article/details/77941277

    相关文章

      网友评论

          本文标题:mac下nginx的安装、使用、配置SSL

          本文链接:https://www.haomeiwen.com/subject/bbfenctx.html