Nginx介绍+openresty配置

参考:资源下载 Nginx介绍+openresty配置

nginx使用场景

1.什么是nginx
	性能高,官方测试5万并发连接;对cpu 内存资源消耗很低,而且运行非常稳定   免费 开源

2.nginx应用场景
	1.http服务器
		静态资源  图片 js css 
	2.虚拟主机
		"虚拟"出多个主机,  域名80    www.51mmr.net   av.51mmr.com   目录 
		IP  端口 域名
	3.反向代理
		正向代理  简单说 代理服务器  从内-->外  
		反向代理   	代理服务器    外-->4.负载均衡
	  字面上理解:负载要变得均衡,负载理解为:工作量 

nginx安装

1.nginx安装
	下载:http://nginx.org/en/download.html
	wget http://nginx.org/download/nginx-1.12.2.tar.gz
2.安装前提
	因为nginx是c开发的
	
	1.gcc
		对源码进行编译
		yum install gcc-c++
	2.PCRE
		perl库 包括了perl兼容的正则表达式 nginx的http模块使用的是pcre来解析正则
		yum install -y pcre pcre-devel

	3.zlib
		压缩和解压的库  nginx 使用的zlib对http包进行 gzip 
		yum install -y zlib zlib-devel

	4.openssl
		yum install -y openssl openssl-devel

3.nginx安装
	1.解压
	 	tar -zxvf nginx-1.12.2.tar.gz 
	2.cd 到解压目录
		 cd nginx-1.12.2
	3.configure
	./configure
		
./configure --prefix=/app/nginx \
--with-pcre \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_ssl_module \
--with-file-aio \
--with-debug \


会生成MakeFile
	4.编译安装
		make && make install
	5.启动nginx
		cd /app/nginx/
		cd sbin	
		./nginx 
		此种方式默认加载的是conf/nginx.conf配置文件
	6.修改加载配置文件
		./nginx -c /app/nginx/conf/nginx.conf
		
	7.关闭nginx
		./nginx -s stop
		先查出pid 然后再kill
		
	方式二:完整停止 优雅关闭
		./nginx -s quit
		等待nginx处理进程把请求处理完毕 然后再停止
	8.重启
		./nginx -s reload
		
		./nginx -s quit
		./nginx
	

Nginx功能
正向代理:从局域网访问Nginx,成功访问到公网。(代理服务器)
反向代理:公网通过连接Nginx,成功访问到局域网。(代理客户端)
负载均衡:Nginx对大量的访问作转发,让这些访问量均衡的转发到不同服务器,来实现负载均衡。
PCRE apt install pcre pcre-devel
:这个库包含了perl库,nginx的http模块使用的是pcre来解析正则表达式
zlib apt install zlib zlib-devel
nginx使用zlib对http包进行解压和压缩
./nginx 启动
关闭nginx
./nginx -s stop //先查处pid,然后kill
./nginx -s quit //等nginx处理完毕进程所有请求后,再关闭,称为优雅关闭
./nginx -s reload //重新加载配置文件

虚拟主机的几种方法

把一台物理机划分成多个虚拟的服务器,每个服务器都可以有独立的域名和目录。Nginx代理这些虚拟主机,将请求转发给他们。
有几种配置虚拟主机的方法:
1.基于IP,几乎不用,公网ip费用高
2.基于端口,很少用,比如http://www.sherlock.net //80端口为默认可以省略 http://www.sherlock.net:8080,只需要复制conf/nginx.conf里的server,然后换不同的端口即可。

虚拟主机的配置
	1.ip
		一台服务器绑定多个ip
	ifconfig	
		绑定一个192.168.101.103
		/sbin/ifconfig eth0:1 192.168.101.103  broadcast 10.117.87.255  netmask 255.255.248.0 up
		/sbin/route add-host 192.168.101.103 dev eth0:1
		服务器重启就会失效
	方式二:
		/etc/sysconfig/network-scripts/ifcfg-eth0 文件复制一份 命名为ifcfg-eth0:1
		
		DEVICE=eth0
		ONBOOT=yes
		BOOTPROTO=static
		IPADDR=192.168.101.103
		NETMASK=255.255.248.0
		服务器重启生效
	
	nginx配置
		一个server就是一个虚拟主机
		
	server {
		        listen       80;
		        server_name  192.168.101.100;

		        #charset koi8-r;

		        #access_log  logs/host.access.log  main;

		        location / {
		            root   html;
		            index  index.html index.htm;
		        }

		   
		        error_page   500 502 503 504  /50x.html;
		        location = /50x.html {
		            root   html;
		        }

		       
		    }

			
			 server {
		        listen       80;
		        server_name  192.168.101.103;

		        #charset koi8-r;

		        #access_log  logs/host.access.log  main;

		        location / {
		            root   html;
		            index  index.html index.htm;
		        }

		   
		        error_page   500 502 503 504  /50x.html;
		        location = /50x.html {
		            root   html;
		        }

		       
		    }
	

	重启nginx  ./nginx -s reload 

	2.端口
		添加一个server 就相当于 添加了一个虚拟主机
		修改listen 改为8077  
		修改root 指定你虚拟主机的跟目录


		server {
		        listen       80;
		        server_name  localhost;

		        #charset koi8-r;

		        #access_log  logs/host.access.log  main;

		        location / {
		            root   html;
		            index  index.html index.htm;
		        }

		   
		        error_page   500 502 503 504  /50x.html;
		        location = /50x.html {
		            root   html;
		        }

		       
		    }

			 
		   server {
		        listen       8077;
		        server_name  localhost;

		        #charset koi8-r;

		        #access_log  logs/host.access.log  main;

		        location / {
		            root   html2;
		            index  index.html index.htm;
		        }

		   
		        error_page   500 502 503 504  /50x.html;
		        location = /50x.html {
		            root   html;
		        }

		       
		    }

		

以上两种配置不足:
	公网ip本省就是一个稀缺资源 
	端口号 我们上线都要求是80 
基于域名的虚拟主机配置(真实线上使用的方案)



3.基于域名

基于域名的虚拟主机

nginx配置
	conf/nginx.conf
	修改server_name 域名
	如果没有域名呢,可以修改window hosts文件 添加对于的ip 

   server {
        listen       80;
        server_name  www.51mmr.net;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }

	 
   server {
        listen       80;
        server_name  av.51mmr.net;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html2;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }

配置反向代理

1.nginx的反向代理
	
2.需求,局域网里有两台tomcat服务器,配置一台nginx反向代理他们,公网设备访问以下两个网址,nginx分别代理到这两个服务器上。
	http://8066.51mmr.com  #域名需要备案
	http://8077.51mmr.com
	
	安装了nginx的服务器:
			 tracking4:
				公网iP:120.26.247.73
				内网IP:10.117.85.48

	安装了tomcat两台服务器
			tracking2:8066
			
				内网IP:10.117.61.252
			datav:8077
		
				内网ip:10.117.183.170




nginx配置:
	=========================================	
	upstream tomcat8066{
	#内网ip
		server	10.117.61.252:8066;
	}
	

	upstream tomcat8077{
	#内网ip
		server	10.117.183.170:8077;
	}
	
	
	
	server {
        listen       80;
        server_name  8066.mmr.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          #反向代理使用了nginx的proxy_pass功能
		   proxy_pass http://tomcat8066; #通过这里找到上面的upstream tomcat8066进而访问到局域网的10.117.61.252:8066 
			
            index  index.html index.htm;
        }

   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }
	
	#多一个server就是多一个虚拟服务器
	server {
        listen       80;
        server_name  8077.mmr.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          
		   proxy_pass http://tomcat8077;
			
            index  index.html index.htm;
        }

   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }

=========================================

配置负载均衡

其实也就是配置正向代理,代理两台服务器,这两台服务器可以跟nginx是同一局域网的,也可以是不同区域有公网IP的。只要nginx能访问到他们就能代理他们。nginx不关注自己代理的是客户端还是服务器,它其实代理的是它能访问到的ip,把流量转发到这些ip上。

1.需求
	请求:http://slb.mmr.com
	需要配置hosts文件
	
	安装了nginx的服务器:
			 tracking4:
				公网iP:120.26.247.73
				内网IP:10.117.85.48

	安装了tomcat两台服务器
			tracking2:8066 16cpu
				公网IP:120.26.216.148
				内网IP:10.117.61.252
			datav:8077 	2cpu
				公网ip:121.41.19.63
				内网ip:10.117.183.170

server load balance  SLB
2.nginx配置
	upstream slb{
	#内网ip
		server	10.117.183.170:8077;
		server  10.117.61.252:8066;
	}
	
	
	server {
        listen       80;
        server_name  slb.hikt.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          
		   proxy_pass http://slb;
			
            index  index.html index.htm;
        }

   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }
-----------------------------------------------------
配置权重

	默认是weight=1 越高处理的请求 越多

	upstream slb{
	#内网ip
		server	10.117.183.170:8077 weight=1;
		server  10.117.61.252:8066   weight=4;
	}
	

定义负载均衡的状态:
1.	upstream myServer {   
2.	
3.	    server 192.168.0.102:9090 down; 
4.	    server 192.168.0.101:8080 weight=2; 
5.	    server 192.168.0.100:6060; 
6.	    server 192.168.0.106:7070 backup; 
7.	}

down :表示当前的server暂时不参与负载
weight:默认1 weight越大 负载的权重就越大
backup:所有非backup的机器down或者忙的时候,才会请求backup机器,所以这台机器压力会很小
max_fails:准许请求失败的次数 默认是1 超过设定的失败次数 就会返回proxy_next_upstream 模块定义 的错误
fail_timeout :  max_fails 次 失败后 (服务器挂了,就不要把请求转给他; ,设置的暂停请求时间,这段时间不会有转发过来) 暂停时间;

负载均衡的session解决

问题:如果在一台服务器上登陆了,这台服务器保留了用户名密码,如果下次刷新登陆在另一台服务器上了,那么还需要重新登陆一遍。这就是记录这些用户名和密码的session问题。
解决方案:

1.session管理
1.session sticky
	根据每次请求 标识 进行转发 保证落到同一台服务器
2.session replication
	session 复制
	
	此种方案 在小量的 服务器 可以使用 
	tomcat 

	网络开销

3.session数据独立
	redis  expire 失效时间
重写session  移动端开发 token    服务端 cookie 

4.开源组件
	tomcat-redis-manager github

con/nginx.conf配置文件详解

1.nginx 虚拟主机的配置 反向代理 负载均衡

2.nginx 配置详细解释

#指定的是用户或者用户组 默认nobody
#user  nobody;

#开启的线程数 一般跟逻辑cpu核数一致
worker_processes  1;

#logs目录是放的日志文件
access.log访问日志 统计uv ip pv 
error.log;错误日志

日志级别:debug info warn error crit   
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程号 id
#pid        logs/nginx.pid;


events {
   #use epoll  ;select poll 
	#每个进程最大的连接数
    worker_connections  1024;
}

#nginx http服务器的配置 
http {

    #主模块指令	mime.types是一段代码,可以直接复制在这里,这样引入有利于排版;也可以一个lua脚本引入进来。就会自动执行。
    include       mime.types;
  #核心模块指令 默认设置为二进制流  	
    default_type  application/octet-stream;

  #日志格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
   #引用日志main
    #access_log  logs/access.log  main;
   #开启高效文件传输
    sendfile        on;

    #开启防止网络堵塞
    #tcp_nopush     on;
   #设置客户端连接保存活的的超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   #一个server就是一个虚拟主机
    server {
        #监听端口号
        listen       8080;
       #主机名	
        server_name  localhost;
        #设置访问的语言编码
        #charset koi8-r;
     #设置虚拟机主机访问的日志文件路径
        #access_log  logs/host.access.log  main;

       #设置虚拟主机的  信息
        location / {
	#根目录 虚拟主机的根目录 /虚拟路径
            root   html;
	# 默认的访问页
            index  index.html index.htm;
        }
	#找不到资源的html
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
       #服务器错误 配置
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
	 #代理服务
        #    proxy_pass   http://127.0.0.1;
        #}
  

    # HTTPS server   80  443
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

openresty安装和第三方模块配置

1.Openresty
	其实就是一个nginx +lua    打包 提供了 包括redis mysql http客户端的大量组件
2.Openresty安装
	https://openresty.org/download/openresty-1.11.2.5.tar.gz
3.准备目录
	mkdir -p /app/servers
	cd /app/servers/
4.安装依赖的组件环境
	yum install -y readline-devel pcre-devel openssl-devel gcc
5.下载
	wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
6.解压
	tar -zxvf openresty-1.11.2.5.tar.gz 
7.进入目录
	cd openresty-1.11.2.5
	cd bundle/LuaJIT-2.1-20170808/
	make clean && make && make install
	安装完 提示执行
	ln -sf luajit-2.1.0-beta3 /usr/local/bin/luajit
	cd ..
回到bundle目录
8.下载模块nxg_cache_purge模块
	在bundle目录下载
	http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
	或者:
	wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz 
	 tar -zxvf 2.3.tar.gz 
9.下载nginx_upstream_check_module模块
	在bundle目录下载
	web健康检查
	wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
	tar -zxvf v0.3.0.tar.gz	

10.进入主目录 编译安装
	cd /app/servers/openresty-1.11.2.5
	./configure --prefix=/app/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
	make && make install
11.进入/app/servers
	cd /app/servers

12.查看和启动
	 /app/servers/nginx/sbin/nginx -V
	/app/servers/nginx/sbin/nginx 	

用lua做一个hello world

1.openresty nginx+lua开发一个helloworld程序

1.修改配置文件
	位置:	/app/servers/nginx/conf/nginx.conf 
	在http部分添加依赖库
	lua_package_path "/app/servers/lualib/?.lua;;";  
	lua_package_cpath "/app/servers/lualib/?.so;;";  

2./app/servers/nginx/conf/目录下创建lua.conf文件 内容如下
	server {
	        listen       8080;
	        server_name  localhost;
			 location /lua {
				default_type 'text/html';
				content_by_lua 'ngx.say("hello world by old wang !")';//使用content_by_lua模块,用lua语法ngx.say()
	        }
		}
3.引入
http {
	lua_package_path "/app/servers/lualib/?.lua;;";  
	lua_package_cpath "/app/servers/lualib/?.so;;";  
	
	include lua.conf;//引入配置文件,注意不是lua脚本,是include引入普通配置的使用
	}

参考课程:ngnix实战&openresty;介绍

### 如何在 IntelliJ IDEA 中配置和使用 Swagger #### 添加 Maven 依赖 为了使 Swagger 能够工作,在 `pom.xml` 文件中需加入特定的依赖项。这可以通过编辑项目的构建文件来完成: ```xml <dependencies> <!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <!-- swagger ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> </dependencies> ``` 这些依赖会引入必要的库用于生成 API 文档以及提供交互式的 UI 页面[^4]。 #### 创建 Swagger 配置类 接着创建一个新的 Java 类用来初始化并配置 Swagger 实例。通常命名为类似于 `SwaggerConfig.java` 的名称,并放置于合适的位置,比如 `config` 包内: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder().title("API文档").description("").termsOfServiceUrl("") .contact(new Contact("", "", "")) .license("").licenseUrl("").version("1.0") .build(); } } ``` 这段代码定义了一个 Spring Bean 来设置 Swagger 的基本信息和其他选项。 #### 启动应用测试 当上述步骤完成后,启动应用程序即可访问默认路径 `/swagger-ui.html` 查看自动生成的 RESTful 接口文档界面。通过浏览器打开该链接可以浏览到所有已暴露出来的 HTTP 请求方法及其参数说明等信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

、、、、南山小雨、、、、

分享对你有帮助,打赏一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值