• centos安装Nginx,反向代理配置全过程


    1.安装依赖

    #gcc安装,nginx源码编译需要
    yum install gcc-c++
    
    #PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
    yum install -y pcre pcre-devel
    
    #zlib安装,nginx 使用zlib对http包的内容进行gzip
    yum install -y zlib zlib-devel
    
    #OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
    yum install -y openssl openssl-devel

    2.下载

    #下载版本号可根据目前官网最新稳定版自行调整
    wget -c https://nginx.org/download/nginx-1.16.1.tar.gz

    3.安装

    #根目录使用ls命令可以看到下载的nginx压缩包,然后解压
    tar -zxvf nginx-1.16.1.tar.gz
    
    #解压后进入目录
    cd nginx-1.16.1
    
    #使用默认配置
    ./configure
    
    #编译安装
    make
    make install
    
    #查找安装路径,默认都是这个路径
    [root@VM_0_12_centos ~]# whereis nginx
    nginx: /usr/local/nginx
    
    #启动、停止nginx
    cd /usr/local/nginx/sbin/
    ./nginx     #启动
    ./nginx -s stop  #停止,直接查找nginx进程id再使用kill命令强制杀掉进程
    ./nginx -s quit  #退出停止,等待nginx进程处理完任务再进行停止
    ./nginx -s reload  #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效
    
    #重启nginx,建议先停止,再启动
    ./nginx -s stop
    ./nginx
    
    #查看nginx进程,如下返回,即为成功
    [root@VM_0_12_centos ~]# ps aux|grep nginx
    root      5984  0.0  0.0 112708   976 pts/1    R+   14:41   0:00 grep --color=auto nginx
    root     18198  0.0  0.0  20552   612 ?        Ss   11:28   0:00 nginx: master process ./nginx
    nobody   18199  0.0  0.0  23088  1632 ?        S    11:28   0:00 nginx: worker process

    直接访问服务器的IP,如果出现以下画面说明成功

     4.配置开机自启动

    如果所用工具能直接打开文件,则可以直接打开修改,不能的话用vi修改

    #在rc.local增加启动代码即可
    vi /etc/rc.local
    #增加一行 /usr/local/nginx/sbin/nginx,增加后保存
    #设置执行权限
    cd /etc
    chmod 755 rc.local

    5.配置映射

    默认配置是80端口,localhost,执行Nginx root目录下的html文件,如果要配多个location,并指向别的文件可参考如下

          location / {
                root   html;
                index  index.html index.htm;
          }
          location  /auth {
               alias /home/webapps/ruqi-web-auth/dist;
               index  index.html index.html;
           }
          location /order {
               alias /home/webapps/ruqi-web-order/dist;
               index  index.html index.html;
          }

    alias是指指向别的目录,设置完,指向命令

    ./nginx -s reload

    就可通过ip/auth,或IP/order,访问相应的项目

    6.反向代理配置解决跨域问题

    前后端分离总是会遇到跨域问题,最后的解决办法无非是后端设置允许跨域访问,或者中间用Nginx或noodle作代理

    接口用统一的一个前缀,例如"api",则可添加如下配置

        location /api/ {
          rewrite  ^.+/api/?(.*)$ /$1 break;
          include  uwsgi_params;
          proxy_pass  http://172.16.205.107:8085/ruqi-auth/;
        }

    意思是所有api接口都会代理到   172.16.205.107:8085/ruqi-auth/  服务器

    7.内容参考

    centos7安装Nginx、使用nginx记录 - 夜的隐为者 - OSCHINA

    利用nginx反向代理解决跨域问题 - 简书

    如果没有Linux环境,也可以在Windows上安装,只是命令不一样

    可参考:nginx入门笔记 - 我又何罪 - 博客园

    我们不生产代码,我们只是代码搬运工Y(^_^)Y

  • 相关阅读:
    arcgis要素转json
    GeoJSON.js:221 Uncaught Error: Invalid GeoJSON object.
    深入学习SAP UI5框架代码系列之一:UI5 Module的懒加载机制
    SAP Spartacus简介
    金庸逝世两周年纪念日:一个失意程序员的呓语
    一个用于SAP UI5学习的脚手架应用,没有任何后台API的依赖
    通过最简单的button控件,深入学习SAP UI5框架代码系列之零
    如何使用SAP UI5 SDK网站查询指定控件的属性如何使用
    一行代码将SAP CDS view数据以ALV的方式输出
    演讲预告:一个月的住院经历,我悟到了哪些和程序员职场发展相关的心得
  • 原文地址:https://www.cnblogs.com/jieker/p/11684062.html
Copyright © 2020-2023  润新知