命令总览:
[root@web01 html]# nginx -h -- 查看nginx帮助选项
[root@web01 7]# nginx -V -- 查看已配置信息,是编译安装是./config配置的内容
[root@web01 html]# nginx -t -- 对配置语法进行检查
# nginx -T 能查看nginx的所有配置信息
[root@web01 html]# nginx -T -- 对配置语法进行检查,并且将所有配置信息显示到屏幕上
[root@web01 html]# nginx -s reload -- -s参数指定需要执行的动作stop, quit, reopen, reload
--(建议和systemctl reload nginx只用一种方式,不要混用)
[root@web01 password]# htpasswd -bc htpasswd oldboy 123456 -- 创建访问认证密码文件
[root@web01 password]# curl www.oldboy.com -u oldboy ## 指定用户,输入密码
[root@web01 password]# curl www.oldboy.com -u oldboy:123456 ## 指定用户及密码
1. nginx的安装部署回顾
2. nginx服务的企业应用 (模块功能)
3. nginx在企业中虚拟主机的访问方式、原理及配置
4. nginx部分总结
链接:https://www.cnblogs.com/moox/p/12689800.html
- 编写虚拟主机配置文件
/etc/nginx/conf.d/*.conf
- 需要获取开发人员编写的网站代码Index.html
- 重启nginx服务(平滑重启) systemctl reload nginx 或 nginx -s reload
- 编写DNS配置信息-- 模拟域名解析windows../hosts
- 进行测试访问
1.1 : 编写虚拟主机配置文件
cd /etc/nginx/conf.d/
vim www.conf
server {
listen 80; # 监听的端口
server_name www.oldboy.com; # 主机的域名
location /oldboy { # 匹配的区域
root /usr/share/nginx/html; # 站点目录
index oldboy.html; # 指定首页文件
}
}
1.2 : 获取开发人员网站代码
上传一张图片moox.jpg到站点目录下/usr/share/nginx/html
<html>
<meta charset="utf-8">
<head>
<title>Nginx head part</title>
</head>
<body>
Nginx 网站服务搭建 body部分
<table border=1>
<tr> <td>01</td> <td>oldboy</td> </tr>
<tr> <td>02</td> <td>oldgirl</td> </tr>
<tr> <td>03</td> <td>olddog</td> </tr>
</table>
<a href="http://cnblos.com/moox">
<img src="moox.jpg" />
</a>
</body>
</html>
1.3 : 重启nginx服务
(平滑重启) 两种方法: -- 选其中一种,不要混用
systemctl reload nginx (yum安装直接平滑重启即可)
nginx -s reload (编译安装无nginx服务,-s指定执行动作)
nginx命令参数(nginx -h)
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-- nginx -t 检查配置语法
-T : test configuration, dump it and exit
-- nginx -T -- 对配置语法进行检查,并且将所有配置信息显示到屏幕上
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-- nginx -s reload 指定执行动作
-p prefix : set prefix path (default: /etc/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
1.4 : 配置DNS域名解析
真实域名: 在阿里云上进行DNS解析记录配置
模拟域名: 在windows主机的hosts文件中进行配置即可
C:\Windows\System32\drivers\etc\hosts
改动频率比较大,可以将hosts快捷方式到桌面
10.0.0.7 www.moox.com
1.5 : 进行测试访问
浏览器中:
http://www.moox.com/ -- 请求的是/ ,默认进入 default.conf中location / 指定的首页
http://www.moox.com/moox.html -- 进入 conf.d/www.conf 中location /moox 指定的首页moox.html
linux:
mkdir /usr/share/nginx/html/moox -p
mv /usr/share/nginx/html/moox.* /usr/share/nginx/html/moox/
systemctl reload nginx
清除缓存重新访问测试:
http://www.moox.com/moox -- 进入 conf.d/www.conf 中location /moox 指定的首页moox.html
网站服务配置文件编写不正确
DNS信息配置不正确
nginx配置文件修改, 一定要重启服务;
站点目录中代码文件信息调整,不需要重启服务
3.1 创建多个虚拟主机配置文件
3.2 创建站点目录和目录中首页文件
3.3 编写hosts解析文件(Windows)
3.4 进行访问测试
3.5. 企业中虚拟主机访问方式
3.1 创建多个虚拟主机配置文件
## /etc/nginx/conf.d/bbs.conf
server {
listen 80;
server_name bbs.oldboy.com;
location / {
root /html/bbs;
index index.html;
}
}
## /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name blog.oldboy.com;
location / {
root /html/blog;
index index.html;
}
}
## /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
}
[root@web01 nginx]# systemctl reload nginx
3.2 创建站点目录和目录中首页文件
[root@web01 conf.d]# mkdir /html/{www,bbs,blog} -p
[root@web01 conf.d]# for name in {www,bbs,blog};do echo "10.0.0.7 $name.moox.com" >/html/$name/index.html ;done
[root@web01 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html ;done
10.0.0.7 www.moox.com
10.0.0.7 bbs.moox.com
10.0.0.7 blog.moox.com
3.3 编写hosts解析文件(Windows)
10.0.0.7 www.oldboy.com bbs.oldboy.com blog.oldboy.com
3.4 进行访问测试
cat /etc/hosts
172.16.1.7 web01 www.moox.com bbs.moox.com blog.moox.com
[root@web01 conf.d]# curl www.oldboy.com
10.0.0.7 www.oldboy.com
[root@web01 conf.d]# curl bbs.oldboy.com
10.0.0.7 bbs.oldboy.com
[root@web01 conf.d]# curl blog.oldboy.com
10.0.0.7 blog.oldboy.com
cat /etc/nginx/nginx.conf
## ...
include /etc/nginx/conf.d/blog.conf; --在包含所有*.conf的前面指定访问顺序
include /etc/nginx/conf.d/*.conf;
1 企业中虚拟主机的访问方式
2 企业中网站的页面访问原理
3 企业中网站的安全访问配置
a 基于域名的方式进行访问:
b 基于地址的方式进行访问: (只能用指定地址访问) --- 负载均衡+高可用服务
server {
listen 10.0.0.7:80; ## 指定IP及端口,重启nginx服务
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
}
## PS: 服务配置文件中涉及到地址修改,必须重启nginx服务,不能平滑重启**
c 基于端口的方式进行访问:
## zabbix服务(apache:80) + web服务(nginx:80) --> 同一台主机
server {
listen 8080;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
}
10.0.0.0/24 www.oldboy.com/AV/ 不能访问 deny
172.16.1.0/24 www.oldboy.com/AV/ 可以访问 allow
nginx访问模块: ngx_http_access_module
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except --- 能够使用deny的区域
[root@web01 conf.d]# cat www.conf
server{
listen 80;
server_name www.moox.com;
location / {
root /html/www;
index index.html index.htm;
}
location /AV {
deny 10.0.0.0/24;
allow 172.16.1.0/24;
}
}
- 补充:
location外面的信息, 全局配置信息
location里面的信息, 局部配置信息
访问时有弹出框,如输入密码认证等
nginx认证模块: ngx_http_auth_basic_module
编写虚拟主机配置文件 /etc/nginix/conf.d/www.conf
nginx认证模块: ngx_http_auth_basic_module
location / {
auth_basic "closed site"; --- 开启认证功能,引号中的内容是提示信息,但需要有auth_basic
auth_basic_user_file conf/htpasswd; --- 加载用户密码文件,密码文件可以指定
}
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
auth_basic "oldboy-sz-01";
auth_basic_user_file password/htpasswd; --- 相对于/etc/nginx目录的相对位置
}
}
htpasswd 创建一个有密文信息的密码文件
htpasswd命令参数说明见文末04.知识总结部分(5)
mkdir /etc/nginx/password/ -p
htpasswd -bc ./password/htpasswd oldboy 123456 -- 免交互式创建加密密码文件
# 命令 -bc:免交互创建 指定文件名 用户名 密码
# 此时浏览器访问需要输入用户名和密码才能访问
[root@web01 conf.d]# ll ./htpasswd
-rw-r--r-- 1 root root 45 Apr 18 16:27 ../password/htpasswd
[root@web01 conf.d]# chmod 600 ./htpasswd
[root@web01 password]# ll
-rw------- 1 root root 45 Apr 18 16:27 htpasswd
## 此时使用的文件权限为root,访问时报500 Internal Server Error
## 一般原因: 500 Internal Server Error
1. 内部程序代码编写有问题
2. 程序服务中文件权限不正确
## 需要将密码文件权限修改为nginx的worker管理的用户www
[root@web01 password]# ps -ef|grep nginx
root 1570 1 0 14:43 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 2260 1570 0 16:34 ? 00:00:00 nginx: worker process
root 2299 1576 0 16:52 pts/0 00:00:00 grep --color=auto nginx
[root@web01 password]# chown www.www htpasswd
[root@web01 password]# ll
-rw------- 1 www www 45 Apr 18 16:27 htpasswd
参数 -u, --user USER[:PASSWORD] Server user and password
[root@web01 password]# curl www.oldboy.com -u oldboy ## 指定用户
Enter host password for user ‘oldboy‘:
10.0.0.7 www.oldboy.com
[root@web01 password]# curl www.oldboy.com -u oldboy:123456 ## 指定用户及密码
10.0.0.7 www.oldboy.com
mkdir /etc/nginx/password/ -p
htpasswd -c ./password/htpasswd oldboy -- 交互式创建加密密码文件
# 命令 -c:创建 指定文件名 用户名
或
htpasswd -bc ./password/htpasswd oldboy 123456 -- 免交互式创建加密密码文件
# 命令 -bc:免交互创建 指定文件名 用户名 密码
或
htpasswd -bpc ./password/htpasswd oldboy 123456 -- 免交互式创建明文不加密密码文件
# 命令 -bpc:免交互创建不加密 指定文件名 用户名 密码
htpasswd -D htpasswd oldboy --- 删除指定用户oldboy密码文件htpasswd,参数-D -删除
# htpasswd命令参数说明:
-c Create a new file. *****
--创建一个密码文件
-n Don‘t update file; display results on stdout.
--不会更新文件; 显示文件内容信息
-b Use the password from the command line rather than prompting for it. *****
--免交互方式输入用户密码信息
-i Read password from stdin without verification (for script usage).
--读取密码采用标准输入方式,并不做检查 ???
-m Force MD5 encryption of the password (default).
--md5的加密算法
-B Force bcrypt encryption of the password (very secure).
--使用bcrypt对密码进行加密
-C Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31).
--使用bcrypt algorithm对密码进行加密
-d Force CRYPT encryption of the password (8 chars max, insecure).
--密码加密方式
-s Force SHA encryption of the password (insecure).
--加密方式
-p Do not encrypt the password (plaintext, insecure).
--不进行加密
-D Delete the specified user.
--删除指定用户
-v Verify password for the specified user.
Nginx:综合架构网站服务 -- nginx网站搭建配置及访问
原文:https://www.cnblogs.com/moox/p/12726926.html