Centos-Linux 安装及配置nginX

安装nginx

1、下载nginx安装包[root@localhost ~]# wget
http://nginx.org/download/nginx-
1.9.9.tar.gz

默认情况,nginx-1.9.9.tar.gz 下载后是放在顶级目录下
[root@localhost ~]# ls //直接 ls 回车就可以看到下载包

切换到顶层目录
[root@localhost ~]# cd //回车

解压 nginx-1.9.9.tar.gz
[root@localhost ~]# tar -zxvf nginx-1.9.9.tar.gz

切换到cd nginx-1.9.9 下面
[root@localhost ~]# cd nginx-1.9.9

执行三个命令:
[root@localhost nginx-1.9.9]# ./configure
[root@localhost nginx-1.9.9]# make
[root@localhost nginx-1.9.9]# make install

切换到 /usr/local/nginx/conf 目录
[root@localhost nginx-1.9.9]# cd /usr/local/nginx/conf

编辑 nginx.conf 文件
[root@localhost conf]# vi nginx.conf

基本配置
server {
    listen 80;
    server_name AMS-WebSite;

    location / {
        proxy_pass     http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}


如果要监听多个站点,可以继续添加 server:
server {
    listen 81;
    server_name Abc-WebSite;
    location / {
        proxy_pass  http://localhost:5080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host; proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        root html;
        index index.html index.htm;
    }
}
 

实现负载均衡:
upstream serverlist {
    #如果采用ip地址来访问,需要加上 ip_hash
    #ip_hash;
    #server 127.0.0.1:8001 weight=2;
    #weight 是访问的权重值,最小为1

    server localhost:8001 weight=2;
    server localhost:8002 weight=1;
}

//注:upstream 后的名称有下划线,可能会报错!
//如:server_list 是非法的,因为带有下划线,解析会报错 

server {
    listen 81;
    server_name TianSense-InputData;

    location / {
        proxy_pass http://serverlist;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        root html;
        index index.html index.htm;
    }
}

nginx 部署 Vue 访问环境
假设 vue 经过执行 npm run build 打包后的 dist 存放路径为 /home/website/dist(以 Linux 为例,  windows系统同理)

server {

    listen        80;

    server_name   website-front;

    location / {

        root   /home/website/dist;

        index index.html index.htm;

    }


   #替换 vue 代理 proxy 访问地址中的 user 标识,对应访问地址 http://localhost:80/user

    location /user {

        proxy_pass         http://localhost:8081/api;

        proxy_http_version 1.1;

        proxy_set_header   Upgrade $http_upgrade;

        proxy_set_header   Connection keep-alive;

        proxy_set_header   Host $host;

        proxy_cache_bypass $http_upgrade;

        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header   X-Forwarded-Proto $scheme;

    }


   #替换 vue 代理 proxy 访问地址中的 permission 标识,对应访问地址 http://localhost:80/permission

    location /permission {

        proxy_pass         http://localhost:8082/api;

        proxy_http_version 1.1;

        proxy_set_header   Upgrade $http_upgrade;

        proxy_set_header   Connection keep-alive;

        proxy_set_header   Host $host;

        proxy_cache_bypass $http_upgrade;

        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header   X-Forwarded-Proto $scheme;

    }

}

Nacos 集群 nginx 设置,针对 nacos 2.0 及以上版本,因为采用了 gRPC 协议,
默认端口号为:8848,9848,注册中心访问端口为 8848,check 用的端口为 9848,相差 1000

nginx.conf 配置文件在解析 stream 标识需要加载 ngx_stream_module.so 模块文件,
如果不存在该文件,需要先下载,步骤如下:
1、yum -y install epel-release
2、yum -y install nginx-all-modules.noarch

cd /usr/lib/nginx/modules ##或 cd /usr/lib64/nginx/modules
ls  #查看目录下是否存在模块文件 ngx_stream_module.so 

在 nginx.conf 首行添加:
load_module /usr/lib64/nginx/modules/ngx_stream_module.so;

nginx.conf 配置:
# http 块之外新增:
# tcp服务
stream {
  # tcp负载均衡
  upstream nacos-cluster-tcp-9022 {
    ip_hash;
    server 192.168.0.2:9848 weight=1;
    server 192.168.0.3:9848 weight=1;
    server 192.168.0.4:9848 weight=1;
  }
  # Nacos客户端gRPC请求服务端端口
  server {
    listen 9022;  #与 8022 相差 1000
    proxy_pass nacos-cluster-tcp-9022;
  }
}

#http 内配置
http {
   upstream nacos-cluster-8022 {
         ip_hash;
         server 192.168.0.2:8848 weight=1;
         server 192.168.0.3:8848 weight=1;
         server 192.168.0.4:8848 weight=1;
   }
   
   server {
      listhen  8022;
      server_name   nacos-cluster-svr
      location / {
           proxy_pass   http://nacos-cluster-8022;
      }
   }
}

保存后
切换目录到 /usr/local/nginx/sbin 下面
[root@localhost conf]# cd /usr/local/nginx/sbin

启动nginx命令:
[root@localhost sbin]# ./nginx

查看是否启动成功
[root@localhost sbin]# ps -ef|grep nginx

设置为开机自动启动
切换到 /etc/systemd/system
[root@localhost sbin]# cd /etc/systemd/system

创建 nginx.service
[root@localhost system]# vi nginx.service

输入以下内容
[Unit]
Description=nginx centos7
After=network.target [Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true [Install]
WantedBy=multi-user.target

按ESC 输入
:wq

让 nginx.service 有效
[root@localhost system]# systemctl enable nginx.service

启动 nginx.service
[root@localhost system]# systemctl start nginx.service

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值