nginx学习

正向代理
在客户端(浏览器)配置代理服务器去访问
隐藏用户自己(隐藏浏览器)

反向代理
客户端访问的是反向代理服务器
隐藏服务器 ( 用户并不知道访问的是反向代理服务器 )

负载均衡
将请求平均分发到不同的服务器上

动静分离
将网页中的 静态资源和 后台资源分开部署 提高用户访问静态代码的速度
降低对后台应用访问

常用命令

1. 启动
	nginx
2. 关闭
	nginx -s stop
3. 重启
	nginx -s reload
4. 测试

配置文件
第一部分 : 全局块
配置文件开始 到 events 主要设置影响nginx服务器整体运行的配置指令

第二部分 events块
	影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多work process下的网络连接进行序列化
	主要配置Nginx服务器与用户的网络连接,常用设置包括:

	是否开启对多work process下的网络连接进行序列化
	是否允许同事接收多个网络连接
	选取事件驱动模型来处理连接请求
	每个word process可以同时支持的最大连接数等
	
第三部分 http块 ( 包含http全局块 server块 )
	#配置最频繁的部分   
		
		1. http全局配置
			http 全局块配置的指令包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等
			可以配置Nginx的代理、缓存和日志定义等大多数功能和第三方模块  包括两部分:http全局快、server块
		2. server
			虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,节省互联网服务器硬件成本
			
			Ⅰ.全局 server块
				最常见的配置是本虚拟机主机的监听配置和本虚拟机主机的名称或IP配置
				这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机完全相同,该技术的产生是为了节省互联网服务器硬件成本。
				每个http块可以包含多个server块,而每个server块就相当于一个虚拟主机。
				而每个server块也分为全局server块,及同时包含多个location块
			
			Ⅱ.location块
				
				一个server块可以配置多个localtion块
				主要作用是基于Nginx服务器接受到的请求字符串( 如 server_name/uri-string ) , 对虚拟主机名称
				( 可以是IP别名) 之外的字符串( 例如前面的 /uri-string ) 进行匹配, 对特定的请求进行处理。地址定向、数据缓存
				和应答控制等功能,还有许多第三方模块的配置也在这里进行。
				
				location [ =|~|~*|^~] uri {
				}
					①. = :全值严格匹配,如果匹配成功则停止继续向下搜索并立即处理该请求;
					②. ~ : uri中正则表达式,区分大小写;
					③. ~* :uri中正则表达式,不区分大小写;
					④. ^~ :uri中不包含的正则表达式,nginx服务器找到匹配度最高的location后,才会使用对应的规则来处理此请求。(也就是可能要全部location都匹配一遍才处理的意思)
					⑤. / :  任何请求都会匹配到
					⑥. !~ !~* : 分别为区分大小写不匹配及不区分大小写不匹配 的正则

nginx配置实例 反向代理
1. 实现效果
a. 打开浏览器,在浏览器地址栏中输入地址 www.123.com, 跳转到linux系统tomcat主页面
2. 访问过程
本地 访问 nginx 配置好的端口 然后 nginx 再访问服务器 ( 服务器端口不再暴露在外面让别人访问 )

3. 具体配置
	a. 首先在 windows system32 drivers etc hosts 中配置 目标域名 + 要改写成的地址
	b. 

nginx配置实例 负载均衡

nginx配置实例 动静分离

nginx配置高可用集群

配置文件 解释

For more information on configuration, see:

* Official English Documentation: http://nginx.org/en/docs/

* Official Russian Documentation: http://nginx.org/ru/docs/

user nginx; # 配置运行Nginx服务器的用户(组)
worker_processes auto; # 处理并发数量值 ( 进程数,并发量 )
error_log /var/log/nginx/error.log; # 日志存放路径
#error_log logs/error.log info; # 日志类型 [ info notice ]
pid /run/nginx.pid; # 进程PID存放路径

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024; # 支持最大连接数 对nginx性能影响较大
}

http {
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"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       80;
    server_name  自己的ip;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /home/dist;
        try_files $uri $uri/ /index.html;
	#   index  index.html index.htm;
    }
    

   location /newsData {
	proxy_pass 自己的ip+端口;
   }
    #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;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

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


# HTTPS server
#
#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;
#    }
#}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值