Nginx的常用场景

静态文件服务器

修改nginx.conf

server {  
  listen 80;  
  server_name localhost;  
  #charset koi8-r;  
  #access_log logs/host.access.log main;  
  location / {  
    #配置静态文件目录 
    root /root/app/;
    #配置首页
    index index.html index.htm;
    #autoindex在浏览器显示文件目录
    autoindex off;
    #限制访问速度
    set $limit_rate 100k;
    }  
}

如果访问出现Nginx 403 Forbidden,在nginx.conf头部添加:

user root;
开启gzip

在http指令块下添加:

gzip on;
gzip_min_length 100;
gzip_comp_level 1;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg img/gif image/pn
g;

反向代理

根据路径后缀配置反向代理到tomcat,启动一台tomcat,请求地址 http://localhost/test1
修改nginx.conf,在serer指令块下添加:

location /test1 {
    proxy_pass http://localhost:8081/;
}

配置成功后,test1后缀的链接都会被代理到Nginx服务器的8081端口上。

负载均衡

修改nginx.conf,在http指令块下添加:

upstream cat {
    server localhost:8081;
    server localhost:8082;
}

server指令块下添加:

location /test {
    proxy_pass http://test/;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值