• centos7上用nginx部署前后端分离项目


    前言

    使用nginx部署前后分离项目,后端为go,前端Vue,部署的服务器环境是CentOS7
    整个流程大抵分为三个块:
    1.安装需要的依赖环境
    2.安装用来部署的nginx
    3.改nginx配置部署项目
    

    1、依赖环境问题

    1.gcc安装
    yum install gcc-c++
    
    # 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
    
    2.PCRE pcre-devel 安装
    yum install -y pcre pcre-devel
    
    # nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
    
    3.zlib 安装
    yum install -y zlib zlib-devel
    
    # nginx 使用 zlib 对 http 包的内容进行 gzip
    
    4.OpenSSL 安装
    yum install -y openssl openssl-devel
    
    

    2、安装Nginx

    1.安装包
    wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
    
    # 解压
    tar -zxvf nginx-1.12.0.tar.gz
    cd nginx-1.12.0
    
    2.配置
    ./configure
    
    3.编译安装
    make
    make install
    
    # 安装路径。whereis nginx
    
    4.启动、停止nginx
    cd /usr/local/nginx/sbin/
    ./nginx 
    ./nginx -s stop
    ./nginx -s quit
    ./nginx -s reload
    
    5.端口占用错误解决
    yum install net-tools
    
    6.配置开机自启动
    vi /etc/rc.local
    
    # 添加
    /usr/local/nginx/sbin/nginx
    
    # 设置权限
    chmod 755 rc.local
    

    3.部署前后分离项目

    1.上传需要部署的项目
    前端项目需要用 npm run build 打包
    找到目录下的 dist 文件夹,上传到需要部署的服务器
    
    后端项目直接上传,记得启动,可编译的项目编译后启动。
    
    2.打开配置文件
    vi /usr/local/nginx/conf/nginx.conf
    
    3.配置里面的server
    daemon on;
    worker_processes  50;
    #error_log /dev/stdout warn;
    error_log  /var/log/nginx/error.log error;
    
    
    events {
        worker_connections 1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        # See http://licson.net/post/optimizing-nginx-for-large-file-delivery/ for more detail
            # This optimizes the server for HLS fragment delivery
        sendfile off;
        #tcp_nopush on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        #access_log /dev/stdout combined;
    
    #     ssl_ciphers         HIGH:!aNULL:!MD5;
    #     ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    #     ssl_session_cache   shared:SSL:10m;
    #     ssl_session_timeout 10m;
    
    server {
            listen 80;
    	server_name xxx.xxx.xx.xxx;
            # Uncomment these lines to enable SSL.
            # Update the ssl paths with your own certificate and private key.
            # listen 443 ssl;
            # ssl_certificate     /opt/certs/example.com.crt;
            # ssl_certificate_key /opt/certs/example.com.key;
            location / {
              root /usr/loacl/xxx;     # 前端的dist文件需要和nginx配置路径相同
              try_files $uri $uri/ /index.html;
              index  index.html;
            }
    	# 添加拦截路径和代理地址
            location /api/ {
              # 添加头部信息
              proxy_set_header X-Forwarded-Proto $scheme;
              proxy_set_header X-Forwarded-Port $server_port;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "upgrade";
              proxy_set_header  Host  $host;
              proxy_pass http://xxx.xxx.xxx.xxx:xxxx/;
            }
    	}
    
    }
    
    
    4.检查配置文件是否正确
    nginx -t
    
    5.重启nginx
    ./nginx -s reload
    
    6.配置注意事项
    # 让nginx拦截所有带/api/的请求,转发到后端服务器,就可以解决跨域的问题
    # location如果一个特定的url 要使用别名,不能用root,alias指定的目录是准确的,root是指定目录的上级目录
    # 注意:使用代理地址时末尾记得加上斜杠"/"。
    # 这里用的root用户,如果用其他用户,可能访问静态资源会被禁止访问(403),需要“chmod 755 静态资源路径 ” 授权,授权的时候要逐级授权,
    
  • 相关阅读:
    prototype属性
    mui 结束时间不能大于开始时间
    DOM方式操作元素属性
    用户输入数字,自动生成钱值
    isNaN函数,判断其参数是否为数字类型
    switch 循环
    淘宝下单高并发解决方案(转)
    NLog文章系列——如何配置NLog(转)
    使用Nlog记录日志到数据库
    NLog文章系列——如何配置NLog
  • 原文地址:https://www.cnblogs.com/leisunny/p/15411050.html
Copyright © 2020-2023  润新知