自学中的对nginx的一些自我认识。
是什么?
代理服务器:一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端。
做什么用?
1、静态资源文件可以部署到此服务器上
2、邮件服务器
3、正向、反向代理、负载均衡
4、动静分离
优点:
1、处理高并发能力
2、配置简单
3、轻量级、占用内存少、稳定性性
windows安装Nginx:
解压 --》cmd进入解压目录 -》执行nginx.exe(启动) 访问localhost即可,默认端口是80
nginx -s stop(强制关闭)|quit(退出)|reload(重新加载配置)|reopen(打开日志文件)
ps aux|grep nginx 查看进程 然后在kill -9 进行号
Linux安装:
1.安装环境:yum -y install gcc openssl-devel pcre-devel zlib-devel
2.下载 wget http://nginx.org/download/nginx-1.10.3.tar.gz 或者上传
3.解压:tar -zxvf 压缩包
4.进入到解压目录进行编译 ./configure --prefix=/usr/nginx 在执行make && make install
5.启动:进入到安装目录/usr/nginx/sbin--》执行./nginx
6.查看进程 ps aux|grep nginx 默认你会看到两个进程,其中进程的用户是nobody,需要修改一下配置文件user root;
7.在访问ip即可,防火墙关闭(防火墙操作-- service iptables status 查看防火墙状态,service iptables stop 关闭防火墙
service iptables start 开启防火墙)
主进程和工作进程:主进程控制工作进程和加载配置文件;工作进程:处理请求、转发、代理等
静态资源文件部署:
在conf目录下修改nginx.conf的http的模块下的server子模块中的location中的root指向的文件夹即可
正向代理 (FQ)
代理服务器代理的是客户端,服务端不知道客户端,客户端是知道服务端
反向代理:
代理服务器代理的是服务端,客户端并不知道服务器是哪一台
nginx反向代理配置:
upstream test{
server:127.0.0.1:8080
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
access_log logs/shsxt.access.log;
error_log logs/shsxt.error.log;
root html;
index index.html index.htm;
## send request back to apache ##
location / {
proxy_pass http://test;
proxy_set_header Host $host:$server_port;
#Proxy Settings
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;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
负载均衡:将流量分摊到多台机器上,根据一些规则(负载均衡的策略--算法)将流量传送到指定的服务器上。
网络七层模型(OSI):物理层、数据链路层、网络层、传输层、会话层、表示层、应用层
可以再第七层负载均衡(nginx)、第四层负载均衡(LVS)、第二层负载均衡(F5)
随机轮询(RR)/ 权重(weight)/ ip_hash:将ip取一个hash值,记录第一次的访问服务器的ip地址,之后所有请求都是在此台机器进行处理
fair:根据响应时间来分配,需要安装第三方模块:https://github.com/gnosek/nginx-upstream-fair
url_hash:根据访问地址进行hash,然后给每一个访问的url地址分配一个服务器:https://github.com/evanmiller/nginx_upstream_hash
Tomcat session共享:可以把Tomcat的Session序列化到redis中,需要三个jar包:https://github.com/jcoleman/tomcat-redis-session-manager
1.序列化中间件jar包
2.需要jedis
3.commons-pool
在context.xml中进行redis的配置
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost" <!-- optional: defaults to "localhost" -->
port="6379" <!-- optional: defaults to "6379" -->
database="0" <!-- optional: defaults to "0" -->
maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.." <!-- optional -->
sentinelMaster="SentinelMasterName" <!-- optional -->
sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />
Nginx动静分离的配置:https://lanjingling.github.io/2015/12/15/tomcat-redis-session/
只需要修改location的拦截,静态请求访问本地静态资源;动态请求访问代理服务器
分布式部署下的Session共享
SpringSession
windows下Redis的安装:
https://github.com/MicrosoftArchive/redis/releases
1、导包 设置spring boot parent,导入spring-booter-starter-redis spring-booter-starter-web spring-session
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、配置: 配置Redis即可
spring.redis.host=localhost---Redis服务器地址
spring.redis.database=1------Redis数据库索引(默认为0)
spring.redis.port=6379--------Redis服务器连接端口
spring.redis.password=---- Redis服务器连接密码(默认为空)
3、开启注解: @EnableRedisHttpSession
4、编写测试代码:把值存入session
@RestController
@RequestMapping("user")
public class UserController {
@RequestMapping("login")
public String login(String userName, String password, HttpSession session) {
// 。。。登录成功
session.setAttribute("userName", userName);
session.setMaxInactiveInterval(60);
return "login success";
}
}
5、在redis客户端进行查看,有没有session的数据
6、在分布式中进行访问:原来的微服务在server端,把数据存入session,在client直接从session里面进获取
7、单点登录SSO
1、使用cookie
2、session共享(web服务端配置共享、应用程序进行session共享)
浙公网安备 33010602011771号