码迷,mamicode.com
首页 > 系统相关 > 详细

linux系统nginx认证加密等模块使用

时间:2020-07-13 21:27:23      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:自动   root   关闭防火墙   too   作用   服务   创建   使用   配置   

课前回顾主要讲述了nginx的安装配置流程

刚开始遇到了一点问题,ping不同外网,但是可以ping通

网关、ip、和dns,解决思路:首先找到网卡配置文件检查ip、网关、和dns,发现都没有问题,然后ping网关、ip、dns,都没有问题,这时候就关闭一个叫做NetworkManager的管理工具包并禁止开机自启,就OK了

安装nginx:首先更换官方源,复制官方配置文件,然后yum下载,安装完成后启动nginx

检查端口和进程,打开浏览器访问默认用的ip访问,前提是没有做域名解析,注意的点是关闭虚拟机的防火墙

配置nginx:vim /etc/nginx/nginx.conf

然后手写server标签

server写法:server{

? 监听的端口

? listen 80;

? server的域名

? server_name www.wzh.com;

? 站点目录和默认页面

? location / {

? root /opt/wzh;

? index index.html index.htm;

}

}

写完后保存退出,然后检查语法,重新加载nginx,根据配置文件的需求创建出站点目录并编辑一个index.html默认页面,接着在hosts文件里做ip和域名解析,然后重新访问浏览器

注:如果在配置文件的站点目录写的是相对路径html的时候默认是在/usr/share/nginx/html/下面,但是一般不写相对路径,容易出错

改名复制小技巧

在xxx后面加上.conf

mv xxx {,.conf}

给xxx备份

cp xxx{,.bak}

关于在server标签配日志:作用域越小优先级越高

比如在全局配置了日志另外在location里面又配置了,会优先选择location里的配置,把location里的日志注释了才会选择全局的,在精细点如果又加入了一个/xxx的日志,那么访问/xxx的时候日志只会记录在/xxx里面

关于状态码:当访问一个网站的时候并不只是加载页面还有很多的比如图片之类的东西,页面可以显示但是别的东西可能加载不出来也会产生404的状态码,所以解决的话就要找到network里面的状态码看看具体的路径,然后把这个路径创建出来

nginx模块

学习nginx模块然后把模块加入nginx的配置文件从而实现页面的各种展示形式和功能

找模块的方式:在浏览器上的网址框里输入nginx.org打开nginx的官方网站,然后点开documentation,往下拉找到Modules reference,下面就是各种模块

index模块:作用就是展示网站的默认页面

Syntax:	index file ...;
Default:	
index index.html;
Context:	http, server, location

注:如果配置文件里有路径但是没有配置index会有403的报错

403:找不到主页

404:找不到页面

** ngx_http_autoindex_module:**

# 自动获取,把浏览器页面变成可预览的目录,默认是off,改成on
Syntax:	autoindex on | off;
Default:	
autoindex off;
Context:	http, server, location
# 把文件大小更精确的输出,默认是on,改成off
Syntax:	autoindex_exact_size on | off;
Default:	
autoindex_exact_size on;
Context:	http, server, location
# 默认显示的格式html
Syntax:	autoindex_format html | xml | json | jsonp;
Default:	
autoindex_format html;
Context:	http, server, location
This directive appeared in version 1.7.9.
# 显示本地时间,默认是off改成on
Syntax:	autoindex_localtime on | off;
Default:	
autoindex_localtime off;
Context:	http, server, location

** ngx_http_charset_module:**字符集模块解决乱码

Syntax:	charset charset | off;
Default:	
charset off;
Context:	http, server, location, if in location

crm学习方式:先抄官网(copy),然后运行(run),最后修改(modify)

手动配置autoindex模块

server {

        listen 80;
        server_name www.wzh.com;
        charset "utf-8,gbk";
        access_log /var/log/nginx/wzh_access.log main;

        location / {
                root /opt/wzh;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime off;
                autoindex_format html;
}
}
# 配置完成后首先检测语法O不OK,然后重新加载nginx,最后再满足配置文件的需求,创建相对应的目录做域名解析,然后再在创建出的站点目录下面创建几个文件,最后访问浏览器
**检测语法**
nginx -t
**重新加载**
systemctl reload nginx
**创建目录**
mkdir /opt/wzh
**做域名解析**
在hosts文件里输入10.0.0.7空格www.wzh.com保存退出
**创建文件**
cd /opt/wzh/
touch www.html
[root@web01 wzh]# touch zzz.html
[root@web01 wzh]# touch hahah.html
[root@web01 wzh]# touch hahah.txt
touch ggg.html
**文件里面写内容**
[root@web01 wzh]# echo 我 >hahah.txt 
[root@web01 wzh]# echo 叫 >hahah.html 
[root@web01 wzh]# echo 王 >ggg.html 
[root@web01 wzh]# echo 张 >zzz.html 
[root@web01 wzh]# echo 行 >www.html
**跟阿里云时间同步**
[root@web01 wzh]# yum install -y ntpdate
[root@web01 wzh]# ntpdate time1.aliyun.com
**浏览器访问**

技术图片

注:**站点目录下面不能创建index.html的文件,如果创建了那么浏览器就是默认找这个页面

模块监控和模块认证

alias:重新定义站点目录,比如我在配置文件里想定义多个站点目录的时候如果用root就会产生报错,这时候改成alias就OK了

nginx的状态监控

# 模块名:ngx_http_stub_status_module记录nginx的状态
Syntax:	stub_status;
Default:	—
Context:	server, location
#写法
location = /basic_status {
    stub_status;
}
#如果使用源码安装的nginx必须加上--with-http_stup_status_module这个选项才可以使用这个模块

认证模块

# 模块名: ngx_http_auth_basic_module
# 写法
location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
}
# 默认是关闭的,改成on
Syntax:	auth_basic string | off;
Default:	
auth_basic off;
Context:	http, server, location, limit_except
# 想使用这个模块要安装httpd-tools,因为这个服务是apache提供的
安装完成会有一个htppasswd命令
这个命令的选项-b和-c
-b:是交互密码
-c:是免交互在命令行输入
-n:可以添加多个用户和-c不能同时使用

练习题

使用五个模块配置server站点**

环境准备

一台新虚拟机ip10.0.0.7

首先关闭防火墙

systemctl stop firewallld

禁止开机自启

systemctl disable firewalld

关闭selinux

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/sysconfig/selinux

然后修改nginx 的源打开nginx.org官网。找到download,然后点进去查看稳定版本,点stable and mainline,找到RHEL/CentOS的配置文件和内容,在虚拟机里面编辑这个配置文件,把内容粘贴进去,保存退出,然后下载nginx,下载完成后开启nginx,并加入开机自启,然后检测端口和进程,没问题就去浏览器访问

然后就是修改nginx的配置文件

编辑nginx的配置文件

[root@web01 conf.d]# vim /etc/nginx/conf.d/www.wzh.conf 
server {
	
	listen 80;
	server_name www.wzh.com;
	charset "utf-8";
	access_log /var/log/nginx/wzh_access.log main;

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

	location /download {
		alias /opt/down;		
		autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;
		#auth_basic           "wzh";
    		#auth_basic_user_file /etc/nfinx/pass/download.pass;
}


	location = /ztjk {
	    stub_status;
}
}
# 配置文件编辑完成后,检测语法nginx -t,
语法没问题就重新加载nginx使用systemctl reload nginx,
加载完成后根据配置文件内容创建对应的站点目录和文件,做域名解析,
然后下载httpd-tools的工具包,
会有一个htpasswd的命令,
使用这个命令的-b和-c选项生成密码然后浏览器访问

技术图片

技术图片
技术图片

技术图片

技术图片
技术图片

技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片
技术图片

linux系统nginx认证加密等模块使用

标签:自动   root   关闭防火墙   too   作用   服务   创建   使用   配置   

原文地址:https://www.cnblogs.com/zabcd/p/13295544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!