nginx模块使用

nginx常用模块

模块解析
–user=nginx用户
–group=nginx组用户
–prefix=/etc/nginx安装路径
–sbin-path=/usr/sbin/nginx程序文件
–modules-path=/usr/lib64/nginx/modules模块路径
–conf-path=/etc/nignx/nginx.conf主配置文件
–error-log-path=/var/log/nignx/error.log错误日志
–http-log-path=/var/log/nginx/access.log访问日志
–pid-path=/var/run/nignx.pid程序ID
–with-http_gunzip_module压缩模块
–whit-http_gzip_static_module压缩模块
–with-http_slice_modulenginx中文文档
–with-http_ssl_module安全模块
–with-http_secure_link_modulenginx安全下载模块
–lock-path=/var/run/nginx.lock所路径,防止重复启动nginx
–http-client-body-temp-path=/var/cache/nginx/client_temp缓存
–http-proxy-temp-path=/var/cache/nginx/proxy_temp代理缓存
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_tempphp缓存
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temppython缓存
–with-compat启动动态模块兼容性
–with-file-aio异步非阻塞模块
–with-threads多线程模块
–with–http_dav_module增加上传PUT,DELETE,MKCOL创建集合,COPY和MOVE方法默认情况下为关闭
–with-http_mp4_module多媒体模块
–with-http_flv_modulenginx添加MP4

url的定义

  • URL地址格式排列为:scheme://host:port/path,例如http://. http://www. sohu.com/domain/HXWZ 就是一个典型的URL地址。. 统一资源定位符 (URL,英语 Uniform / Universal Resource Locator 的缩写)也被称为网页地址,是因特网上标准的资源的地址 (Address)。. 它最初是由蒂姆·伯纳斯-李发明用来作为万维网的地址的。. 现在它已经被万维网联盟编制为因特网标准RFC1738了。. 统一资源定位符 (URL)是用于完整地描述Internet上网页和其他资源的地址的一种标识方法。

stub_status_module:查询状态模块

先查看nginx模块是否被安装

[root@localhost ~]# nginx -V | grep stub_status_module 查看状态模块

配置 nginx的状态模块,stub_status_module

[root@localhost ~]# vim /etc/nginx/conf.d/lb.conf
upstream web_cluster {          ##转发模块
        server 192.168.1.54:80;   ##转发节点
        server 192.168.1.53:80;   ##转发节点
}

server {				          ##服务
        listen 192.168.1.77:80;   ## 监听端口
        server_name blog.benet.com;  ##域名访问

        location / {                 ##url资源定位地址
                proxy_pass http://web_cluster;  ##转发模块
        }
}
server {
        listen 80;
        location /status {            ##url资源定位地址
                stub_status;          ##状态模块
                allow all;            ##权限所有用户都可以访问(可选)
        }
}

打开网页访问网站的状态
在这里插入图片描述

random_index_module:随机主页

前提,有多页index首页。

[root@localhost html]# vim /etc/nginx/conf.d/lb.conf
server {                 ##服务
        listen 80;       ##监控端口
        server_name blog.benet.com;		##域名访问

        location / {		##url资源定位地址
                root /usr/share/nginx/html;    ##访问路径
                random_index on;			##随机访问
         }

}

重新加载nginx,打开游览器,访问网址会发现网站不一样没有规律的变化。

sub_module:替换模块

[root@localhost html]# vim /etc/nginx/conf.d/lb.conf
server {		     ##服务模块
        listen 80;  	##监听端口
        server_name blog.benet.com;   	##域名访问
        sub_filter shei "HEOLL——HEOLL";    ##替换字段
        sub_filter_once off;			##是否第一个替换,on是/off 否

        location / {			##url资源定位地址
                root /usr/share/nginx/html;			##访问路径
                index index.html index.htm index.ht;      ##定义默认访问格式
         }

}

sendfile:文件读取模块

默认的模块是开启的

[root@localhost html]# vim /etc/nginx/nginx.conf
 22     access_log  /var/log/nginx/access.log  main;     ##全访问日志路径
 23
 24     sendfile            on;  		##指令指定nginx是否调用,sendfile函数(zero copy 方式)来输出文件,对于普通应用,必须设为no
 25     tcp_nopush          on;          ##此项允许或者将禁止使用socket的TCP_CORK的选项,此选项仅在使用sendfile的时候使用(要是不开启,网路资源浪费。要是开启,对网络传输效率提升。)
26     tcp_nodelay         on;			  ##直接发送ack报文确定,无需等tcp_nopush的限制。

ngx_http_gzip_module:文件压缩模块

目的:减少带宽,传输速度快。
使用添加gzip压缩中,在配置文件中的http字段中配置。

[root@localhost html]# vim /etc/nginx/nginx.conf
http {
......
	gzip on; 		##开启压缩模式
	gzip_http_version 1.1;		##压缩版本为1.1
	gzip_comp_level 9;			##压缩级别在为0~9
	gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;        ##可以压缩的类型:文本、脚本、脚本、text表、文件、java脚本、httpd-php模块、图片、图像、图像。
	gzip_static on;				##静态压缩是否开启on开/off关
	server {
	 ...... ##设置监听的内容等
	   location / {
	   ...... ##设置访问路径等
	     }
	  }
......

ngx_http_headers_module:缓存模块

缓存模块的使用:在http字段中添加是所有服务都有效,在server中添加是在但这服务中有效,在location中添加只对单个网页有效。

expires 24h; ##缓存24小时(s:秒、min:分钟、h:小时)

防盗链

ngx_http_referer_module:防盗链模块
防盗链链接使用的时候注意,做(单机)实验时,注意不要把盗的文件放在一个地址,要不然会失效。

[root@localhost html]# vim /etc/nginx/conf.d/lb.conf
server {			##服务模块
        listen 192.168.1.56:80;		##监听端口
        server_name www.1.html.com;		##域名定义

        location / {			##url资源地址定位
                root html;		##资源地址
                index index.html index.htm index.htm;    ## 默认访问首页
        }
}

server {			##服务模块
        listen 192.168.1.56:80;		##监听端口
        server_name www.1.jpg.com;		##域名定义

        location /web {			##url资源地址定位
                root html;		##资源地址
                index 1.jpg 1.jp;    ## 默认访问首页



        access_log /var/log/nginx/1.jpg.log main;		##日志存放位置,在server中只针对该服务
        valid_referers none blocked *.1.jpg.com server_name(白名单) 192.168.1.* ~\.google\. www.1.html.com;		##防盗链模块,防止盗用链接。跟白名单配置使用可以有效的防止链接被盗。
        }
}

安全加密https

私有CA的配置文件

server {				##服务模块
        listen 443 ssl;			##监听端口,443端口是ssl加密认证的默认端口
        server_name www.web1.com;		##网站服务域名

        ssl_certificate /etc/nginx/ssl/server.crt; 		##安全证书位置
        ssl_certificate_key /etc/nginx/ssl/server.key;	 ##安全证书密钥
        
        location / {					##url资源地址定位
                root /web/web1;			##网站默认访问位置
                index index.html;		##默认首页格式
        }
}

公网CA配置文件
配置公网CA,需要在阿里云官网上购买公网CA认证。

server {			##服务模块
	listen 80;		##监听端口
	server_name www.xiaowang.com;		##服务域名
	retum 301 https://www.xiaowang.com$request_uri;		##重定向到,https://www.xiaowang.com(原来访问的位置);
}
server {		##服务模块
	listen 443 ssl; 	##监听端口443,ssl加密访问
	ssl on;			##安全加密模块开启
	ssl_certificate /etc/nginx/ssl/server.crt; 		##安全证书位置
     ssl_certificate_key /etc/nginx/ssl/server.key;	 ##安全证书密钥
	location / {			##url资源地址定位
		root   html;		##默认访问地址
		index index.html index.htm;		##默认访问首页地址
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值