nginx相关(负载均衡、反向代理服务器解决前端跨域问题等)

1.安装依赖
yum install gcc
yum install pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel

2.下载解压nginx
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz (下载tar包)
tar -xvf nginx-1.13.7.tar.gz (解压文件)

3.安装nginx
进入nginx目录
cd /usr/local/nginx-1.13.7
执行命令
./configure
执行make命令
make
执行make install命令
make install

4.配置nginx
cd到nginx配置文件,配置相应的信息数据,主要是配置好upstream 以及server即可

5.实例
重定向:
listen 80; #监听80端口
server_name www.dbspread.com; #域名
#rewrite规则
index index.jsp index.html index.htm;
root /usr/local/nginx/html; #定义服务器的默认网站根目录位置
#重定向
if ($host != 'www.dbspread.com' ){
  rewrite ^/(.*)$ http://www.dbspread.com/$1 permanent; #permanent表示永久301重定向标记
}

备份系统:
upstream neiwei_servers {
  server 127.0.0.1:8080;
  server 127.0.0.1:8091 backup;
}

location /waiwei {

  client_max_body_size 100m;
  proxy_connect_timeout 4;
  proxy_send_timeout 1200;
  proxy_read_timeout 1200;
  root /root/web/waiwei/webapps;
  proxy_pass http://waiwei_servers;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header REMOTE-HOST $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

nginx防盗链:
server {
  listen 80;
  server_name www.dbspread.com *.dbspread.com;
  location ~* \.(rmvb|jpg|png|swf|flv)$ { #rmvb|jpg|png|swf|flv 表示对rmvb|jpg|png|swf|flv后缀的文件实行防盗链
    valid_referers none blocked www.dbspread.com; #表示对www.dbspread.com此域名开通白名单,比如在www.test.com的index.html引用download/av123.rmvb,无效
    root html/b;
    if ($invalid_referer) { #如果请求不是从www.dbspread.com白名单发出来的请求,直接重定向到403.html这个页面或者返回403
      #rewrite ^/ http://www.dbspread.com/403.html;
      return 403;
    }
  }

}

动静分离:
#静态分离 ~匹配 以.*结尾(以html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css结尾走这段),当然不是越久越好,如果有10000个用户在线,都保存几个月,系统托跨
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
  root /var/local/static; #静态资源存放在nginx的安装机器上
  #proxy_pass http://www.static.com; #静态资源也可存放在远程服务器上
  expires 30d; #30天之内只要访问过一次就从缓存拿
}

日志:
#日志级别有[debug|info|notice|warn|error|crit] error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit, 生产环境用error
#crit 记录的日志最少,而debug记录的日志最多
access_log /usr/local/logs/web2/access.log main;
error_log /usr/local/logs/web2/error.log crit;

 

posted @ 2019-10-31 16:06  smj1990  阅读(351)  评论(0编辑  收藏  举报