美文网首页
Mac上启用80端口——nginx的安装

Mac上启用80端口——nginx的安装

作者: Melody_YM | 来源:发表于2018-05-31 14:23 被阅读78次

    现在mac上有个新需求,需要tomcat使用80端口而不是8080端口。

    mac上使用80端口需要root权限,所以不能直接使用。而是需要使用nginx,启动nginx后它可以使用80端口,然后映射到tomcat的8080端口上。

    下载并安装nginx。
    Nginx.org网站上下载tar.gz格式的源码包。

    cd ~/Downloads
    tar xvzf nginx-1.9.5.tar.gz 
    cd nginx-1.9.5
    sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/openssl
    sudo make
    

    在make这一步发生了一个错误,提示:make: *** No rule to make target build', needed bydefault'. Stop. 这说明在上一步configure有错误。

    查看configure,发现没有配置prec。

    checking for PCRE library ... not found
    checking for PCRE library in /usr/local/ ... not found
    checking for PCRE library in /usr/include/pcre/ ... not found
    checking for PCRE library in /usr/pkg/ ... not found
    checking for PCRE library in /opt/local/ ... not found
    

    所以需要提前安装 PCRE库。

        PCRE的链接[https://www.openssl.org/source/openssl-1.0.2l.tar.gz](https://www.openssl.org/source/openssl-1.0.2l.tar.gz)
    
      sudo tar xvfz pcre-8.36.tar.gz 
      cd pcre-8.36
      sudo ./configure --prefix=/usr/local --enable-utf8 
      sodu make
      sudo make
      sudo make install
    

    还要安装openssl。

    tar xvzf openssl-1.0.2a.tar.gz 
       cd openssl-1.0.2a
      ./config --prefix=/usr/local/openssl
      
      在make之前,在openssl-1.0.2a目录下找到Makefile这个文件,用编辑器打开查找,将darwin-i386-cc替换成darwin64-x86_64-cc,保存。
      
      sudo make
      sudo make install
      sudo ln -s /usr/local/openssl/bin/openssl /usr/local/openssl 
      openssl version
    

    openssl安装完毕。

    接着返回nginx的configure,结果发现还报错!

    原来编译命令也就是 with-openssl=/______这个路径指向你的源码安装包路径而不是你安装后的路径!

      sudo ./configure --prefix=/usr/local/nginx --with-openssl=/Users/lizhengdong/Downloads/openssl-1.0.2a
      make
      sudo make
      sudo make install
      
      加入环境变量
      ~/.bash_profile  (一般在这个文件中添加用户级环境变量)
      添加下面环境变量:
      export PATH=${PATH}:/usr/local/nginx/sbin
      
      nginx -V
    nginx version: nginx/1.9.5
    built by clang 7.0.0 (clang-700.1.76)
    configure arguments: --prefix=/usr/local/nginx --with-openssl=/Users/lizhengdong/Downloads/openssl-1.0.2a
    

    安装完毕,启动nginx试一下,sudo nginx,在浏览器里输入localhost,如下图

    image.png

    说明nginx没问题,sudo nginx -s stop 关闭nginx。

    进入nginx目录/usr/local/nginx,打开nginx.conf。

    image.png 改成下图这样。proxy_pass,它表示代理路径,相当于转发。 image.png

    修改完成后运行了nginx -s reload进行重新加载配置文件

    重启nginx,欧克了!

    localhost:8080换成其他url的时候,前面再加一些配置。

     proxy_redirect     off;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            autoindex off;
    ```![image.png](https://img.haomeiwen.com/i1442189/7af0355b4fa5dfc2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
    
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:Mac上启用80端口——nginx的安装

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