Nginx动静分离的配置

一、nginx安装(全部服务器执行)

1.1 安装依赖包

yum install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
yum install wget

1.2 下载nginx安装包

[root@node1 quartz]# cd /usr/local/
[root@node1 quartz]# wget https://nginx.org/download/nginx-1.15.3.tar.gz
[root@node1 quartz]# tar -zxvf nginx-1.15.3.tar.gz

1.3 安装nginx

//进入nginx目录
[root@node1 quartz]# cd /usr/local/nginx-1.15.3
//执行命令
[root@node1 quartz]# ./configure
[root@node1 quartz]#  make
[root@node1 quartz]# make install
[root@node1 quartz]# /usr/local/nginx/sbin/nginx -s reload
[root@node1 quartz]# ps -ef | grep nginx  //查看nginx进程是否启动:

1.4 nginx命令

/usr/local/nginx/sbin/nginx -s reload        # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen        # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop          # 停止 Nginx    

二、nginx配置文件

2.1 修改主配置文件 nginx.conf

  在http{ }中添加引入外部的conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;

    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  /data/logs/nginx/access.log  main;

    #tcp_nopush     on;
    ssi                 on;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   60;
    types_hash_max_size 2048;
    client_max_body_size  1000m;

    #gzip  on;
    include vhost/*.conf;
    #server {
    #    listen       80;
    #    server_name  localhost;
    #        stub_status            on;
    #        access_log             off;
    #    }

    # error_page   500 502 503 504  /50x.html;

}

2.2 创建vhost文件夹,存放多个conf

 

2.3 创建conf文件

upstream cms-api {
   server ip1:端口号;
   server ip2:端口号;
   keepalive 64;
}

server {
        listen       80;
        server_name  域名;

        root 前端路径;
        try_files $uri /index.html;

        access_log /data/logs/nginx/cms.log;
        error_log /data/logs/nginx/cms.error.log;

        location /api/ {
                proxy_pass http://cms-api/;
                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;
                add_header Cache-Control max-age=300;
        }

}

 

posted @ 2021-02-21 11:32  TIMEAWAY  阅读(235)  评论(0编辑  收藏  举报