标签:www 服务 nginx 网站 Nginx html root localhost
目录
前言
随着计算机与Internet技术的蓬勃发展,各种Web站点成为直接面向用户的中坚力量,在各种网站服务器软件中,除了Apache外,还有一款轻量级的HTTP服务器软件——Nginx,其稳定性、高效的特性逐渐被越来越多的用户认可。本篇博客将搭建Nginx网站服务器,并配置基于域名的虚拟Web主机。
一、关于Nginx
1、概述
Nginx是由俄罗斯的Igor Sysoev专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力(单台物理服务器可支持30 000~50 000个并发请求)。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供Web服务。
2、优点
- 稳定性高
- 系统资源消耗低
- 对HTTP并发连接的处理能力高
- 单台物理服务器可支持30000~50000个并发请求
3、Nginx和Apache的对比
- Nginx是一个基于事件的Web服务器,Apache是一个基于流程的服务器
- Nginx所有请求都由一个线程处理,Apache单个线程处理单个请求
- Nginx避免子进程的概念,Apache是基于子进程的
- Nginx在内存消耗和连接方面更好,Apache在内存消耗和连接方面一般
- Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于CPU和内存等硬件
- Nginx支持热部署,Apache不支持热部署
- Nginx对于静态文件处理具有更高效率,Apache相对一般
- Nginx在反向代理场景具有明显优势,Apache相对一般
二、安装Nginx服务
1、解压源码包到/opt下,并查看
[root@localhost ~]# cd /mnt ##切换到挂载点目录
[root@localhost mnt]# ls
apr-1.6.2.tar.gz Discuz_X2.5_SC_UTF8.zip LAMP-php5.6.txt
apr-util-1.6.0.tar.gz error.png mysql-5.6.26.tar.gz
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2 nginx-1.12.0.tar.gz
cronolog-1.6.2-14.el7.x86_64.rpm kali.jpg php-5.6.11.tar.bz2
[root@localhost mnt]# tar zxvf nginx-1.12.0.tar.gz -C /opt ##解压Nginx源码包到/opt下
[root@localhost mnt]# cd /opt/ ##切换到解压的目录下
[root@localhost opt]# ls
nginx-1.12.0 rh
2、安装编译须要的环境组件包
[root@localhost opt]# yum -y install \
gcc \ //c语言
gcc-c++ \ //c++语言
pcre-devel \ //pcre语言工具
zlib-devel //数据压缩用的函式库
3、 建立程序用户nginx并编译Nginx
[root@localhost opt]# useradd -M -s /sbin/nologin nginx ##建立程序用户,安全不可登录状态
[root@localhost opt]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
[root@localhost opt]# cd nginx-1.12.0/ ##切换到nginx目录下
[root@localhost nginx-1.12.0]# ./configure \ ##配置nginx
> --prefix=/usr/local/nginx \ ##安装路径
> --user=nginx \ ##用户名
> --group=nginx \ ##用户组
> --with-http_stub_status_module ##状态统计模块
4、编译和安装
make -j3 && make install
5、优化nginx启动脚本,以便于系统识别
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##建立软链接让系统识别nginx启动脚本
[root@localhost nginx]# nginx -t ##检查配置文件的语法问题
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx ##开启ngnix
[root@localhost nginx]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost nginx]# setenforce 0
6、启动、重新配置、停止Nginx
[root@localhost nginx-1.15.9]# nginx ## 启动 Nginx
[root@localhost nginx-1.15.9]# netstat -anpt |grep nginx ## 过滤Nginx的进程
[root@localhost ~]# yum -y install psmisc ###最小安装没有killall令需要安装
[root@localhost ~]# killall -s HUP nginx ## 重载Nginx配置文件(相当于刷新)
[root@localhost ~]# killall -s QUIT nginx ## 退出 Nginx
7、Nginx服务控制文件使用systemctl工具
[root@localhost ~]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx ###描述
After=network.target ####描述服务类别
[Service]
Type=forking ###后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ###PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ###启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
==>> wq 保存
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service
[root@localhost ~]# systemctl start nginx ## 这样我们就可以用这种方法来开启 Nginx 了
8、测试
二、Nginx访问控制
1、访问状态统计
Nginx 内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的Web访问情况,配置编译参数时可添加--with-http_stub_status_module来启用此模块支持
可使用命令/usr/local/nginx/sbin/nginx-V来查看已安装的Nginx是否包含HTTP_STUB_STATUS模块。
要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf配置文件,指定访问位置并添加stub_status 配置代码。
vim /usr/local/nginx/conf/nginx.conf
//增加 stub_status 配置
location /status { //访问位置为/status
stub_status on; //打开状态统计功能
access_log off; //关闭此位置的日志记录
}
重启服务,访问测试
nginx -t
systemctl restart nginx.service
netstat -natp | grep 80
2、访问控制
2.1 基于授权的访问控制
- 先安装 httpd-tools
[root@localhost ~]# yum -y install httpd-tools
- 创建用户 test 并设置密码 12345
[root@localhost ~]# htpasswd -c /usr/local/nginx/.passwd.db test
New password:
Re-type new password:
Adding password for user test
- 修改文件权限
[root@localhost ~]# chmod 400 /usr/local/nginx/.passwd.db ## 修改密码文件的权限为400
[root@localhost ~]# chown nginx /usr/local/nginx/.passwd.db
将所有者修改为 nginx ,设置nginx的运行用户能够读取
- 查看存放用户名和密码的文件
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# cat .passwd.db ##查看存放用户名和密码的文件
test:$apr1$vHVaACQT$i1sRjEd2M59E4EJfpxliA.
- 修改配置文件,添加认证配置
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf ##修改配置文件
添加
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/.passwd.db;
==>> wq 保存
[root@localhost ~]# nginx -t ##检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx ## 重启服务
2.2 基于客户端的访问控制
- 通过客户端IP地址,决定是否允许对页面访问
- 配置规则
■deny lP/IP段:拒绝某个IP或IP段的客户端访问
■allow IP/IP段:允许某个IP或IP段的客户端访问
■规则从上往下执行,如匹配则停止,不再往下匹配
修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
43 location / {
44 auth_basic "secret"; //44,45行删除掉
45 auth_basic_user_file /usr/local/nginx/passwd.db;
46 root html;
47 index index.html index.htm;
48 deny 192.168.41.160; //增加拒绝访问的客户端的IP
49 allow all; //增加允许其他IP客户端访问
50 }
重启服务,访问测试
nginx -t
systemctl restart nginx
三、Nginx虚拟主机
1、基于域名的Nginx虚拟主机
- 添加两个域名,它们都指向同一个服务器IP地址,用于实现不同的域名访问不同的虚拟主机
vim /etc/hosts
192.168.41.140 www.520.com www.accp.com www.benet.com //在后面增加两个域名
为虚拟主机准备网页文档
mkdir -p /var/www/html/accp //创建www.accp.com的根目录
mkdir -p /var/www/html/benet //创建www.benet.com的根目录
echo "<h1> www.accp.com<h1>" >/var/www/html/accp/index.html
echo "<h1> www.benet.com<h1>" >/var/www/html/benet/index.html
修改Nginx的配置文件
35 server {
36 listen 80;
37 server_name www.accp.com; //设置域名www.acc0.com
38 charset utf-8;
39 access_log logs/accp.access.log; //设置日志名
40 location / {
41 root /var/www/html/accp/; //设置www.accp.com的工作目录
42 index index.html index.htm;
43 }
44 error_page 500 502 503 504 /50x.html;
45 location = /50x.html {
46 root html;
47 }
48 }
49
50 server {
51 listen 80;
52 server_name www.benet.com;
53 charset utf-8;
54 access_log logs/benet.access.log;
55 location / {
56 root /var/www/html/benet/;
57 index index.html index.htm;
58 }
59 error_page 500 502 503 504 /50x.html;
60 location = /50x.html {
61 root html;
62 }
63 }
重启服务,访问测试
nginx -t
2、基于端口的虚拟机
创建8080端口的网页文件
mkdir -p /var/www/html/ll8080
echo "<h1> www.ll8080.com </h1>" > /var/www/html/ll8080/index.html
修改nginx主配置文件,仅修改监听端口
35 server { //原accp配置
36 listen 192.168.41.140:80; //指向监听端口
37 server_name www.accp.com;
38 charset utf-8;
39 access_log logs/accp.access.log;
40 location / {
41 root /var/www/html/accp/;
42 index index.html index.htm;
43 }
44 error_page 500 502 503 504 /50x.html;
45 location = /50x.html {
46 root html;
47 }
48 }
49
50 server { //新accp配置
51 listen 192.168.41.140:8080; //指向8080端口
52 server_name www.accp.com;
53 charset utf-8;
54 access_log logs/accp8080.access.log; //便于区分,指定生成不同日志
55 location / {
56 root /var/www/html/accp8080; //指向8080端口的站点首页
57 index index.html index.htm;
58 }
59 error_page 500 502 503 504 /50x.html;
60 location = /50x.html {
61 root html;
62 }
63 }
3、基于不同IP访问
临时创建虚拟网卡
ifconfig ens33:0 192.168.41.100 netmask 255.255.255.255
增加192.168.41.100的映射
vim /etc/hosts
192.168.41.100 www.benet.com
创建网站根目录、创建192.168.41.100的网站首页文件(index.html)
mkdir /var/www/html/benet100
echo "<h1> www.benet100.com </h1>" >>/var/www/html/benet100/index.html
修改配置文件
35 server {
36 listen 192.168.41.140:80;
37 server_name www.accp.com;
38 charset utf-8;
39 access_log logs/accp.access.log;
40 location / {
41 root /var/www/html/accp/;
42 index index.html index.htm;
43 }
44 error_page 500 502 503 504 /50x.html;
45 location = /50x.html {
46 root html;
47 }
48 }
49
50 server {
51 listen 192.168.41.100:80; //benet监听的IP修改为100
52 server_name www.accp.com;
53 charset utf-8;
54 access_log logs/benet100.access.log;
55 location / {
56 root /var/www/html/benet100;
57 index index.html index.htm;
58 }
59 error_page 500 502 503 504 /50x.html;
60 location = /50x.html {
61 root html;
62 }
63 }
重启服务,访问测试
nginx -t
systemctl restart nginx.service
netstat -antp | grep nginx
总结
- Nginx 内建的访问统计功能由stub_status模块提供,需要在编译时启用“--with-http_stub_status_module”选项。
- Nginx 页面访问安全有基于授权和基于客户端两种方式。
- Nginx 虚拟主机搭建可基于IP、域名和端口。
标签:www,服务,nginx,网站,Nginx,html,root,localhost 来源: https://blog.csdn.net/HB199753/article/details/120592404
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。