我是靠谱客的博主 自信小懒猪,最近开发中收集的这篇文章主要介绍Nginx访问状态以及基于多域名、多端口、多IP配置虚拟主机一、关于Nginx二、Nginx的优化服务三、配置Nginx虚拟主机,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
目录
- 一、关于Nginx
- 二、Nginx的优化服务
- 2.1 编译安装
- 2.2 Nginx访问状态的统计
- 2.3 Nginx身份验证访问
- 三、配置Nginx虚拟主机
- 3.1 基于域名
- 3.2 基于IP
- 3.3 基于端口
一、关于Nginx
- 一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
- 对HTTP并发连接的处理能力高
单台物理服务器可支持30000~50000个并发请求
二、Nginx的优化服务
2.1 编译安装
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar zxf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure
> --prefix=/usr/local/nginx
> --user=nginx
> --group=nginx
> --with-http_stub_status_module #统计状态模块
[root@localhost nginx-1.12.2]# make && make install
[root@localhost ~]# useradd -M -s /sbin/nologin nginx #创建一个不可登录的程序用户
[root@localhost ~]# ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin #优化执行路径
[root@localhost ~]# nginx #启动服务
[root@localhost ~]# netstat -anpt | grep ngin
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3580/nginx: master
[root@localhost ~]# killall -s QUIT nginx #选项-s QUIT等于-3 停止服务
[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
2.2 Nginx访问状态的统计
- 配置文件修改
[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx #修改并删掉"#"号
error_log logs/error.log info #删掉"#"号
events {
use epoll; #新增此行,默认使用select/poll
worker_connections 1024; #表示一个工作进程允许1024个连接
}
location ~ /status {
stub_status on;
access_log off;
} #在配置统计功能
[root@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@nginx ~]# nginx -V #检查开启的模块
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx ~]# systemctl restart nginx
- 结果
2.3 Nginx身份验证访问
- 创建能够访问web网站的用户
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# which htpasswd
/usr/bin/htpasswd #htpasswd命令需要先安装httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db ben
New password:
Re-type new password:
Adding password for user ben
[root@localhost ~]# ll /usr/local/nginx/ | grep passwd.db
-rw-r--r--. 1 root root 42 Nov 16 11:22 passwd.db
[root@localhost ~]# chown nginx.nginx /usr/local/nginx/passwd.db
[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
[root@localhost ~]# vi /etc/nginx
location / {
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
root html;
index index.html index.htm;
}
[root@localhost ~]# systemctl restart nginx
- 验证
- 拒绝访问
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
[root@localhost ~]# vi /etc/nginx
location / {
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
deny all;
root html;
index index.html index.htm;
}
[root@localhost ~]# systemctl restart nginx
- 结果验证
三、配置Nginx虚拟主机
3.1 基于域名
- 修改配置文件
[root@nginx ~]# vi /etc/nginx
server {
listen 80;
server_name www.test-1.com; #localhost修改成www.test-1.com
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/test-1/;
index index.html index.htm;
}
server {
listen 80;
server_name www.test-2.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/test-2/;
index index.html index.htm;
}
}
[root@nginx ~]# systemctl restart nginx
在Windows 10的hosts里添加
10.0.0.71 www.test-1.com www.test-2.com
- 创建不同网页
[root@localhost ~]# mkdir -p /var/www/test-1
[root@localhost ~]# mkdir -p /var/www/test-2
[root@localhost ~]# echo "<center>Web-1</center>" > /var/www/test-1/index.html
[root@localhost ~]# echo "<center>Web-2</center>" > /var/www/test-2/index.html
- 结果验证
3.2 基于IP
- 修改配置文件
[root@nginx ~]# ip a | grep '/16' | awk '{print $2}' | awk -F '/' '{print $1}'
10.0.0.71
10.0.0.108
[root@nginx ~]# vi /etc/nginx
server {
listen 10.0.0.71:80;
server_name www.test-1.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/test-1/;
index index.html index.htm;
}
server {
listen 10.0.0.108:80; #修改成另一个IP地址
server_name www.test-2.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/test-2/;
index index.html index.htm;
}
}
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# netstat -anpt | grep nginx
tcp 0 0 10.0.0.108:80 0.0.0.0:* LISTEN 1091/nginx: master
tcp 0 0 10.0.0.71:80 0.0.0.0:* LISTEN 1091/nginx: master
- 结果验证
3.3 基于端口
- 修改配置文件
[root@nginx ~]# vi /etc/nginx
server {
listen 10.0.0.71:80;
server_name www.test-1.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/test-1/;
index index.html index.htm;
}
server {
listen 10.0.0.71:8008; #开放8008端口
server_name www.test-2.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/test-2/;
index index.html index.htm;
}
}
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# netstat -anpt | grep nginx
tcp 0 0 10.0.0.71:80 0.0.0.0:* LISTEN 1123/nginx: master
tcp 0 0 10.0.0.71:8008 0.0.0.0:* LISTEN 1123/nginx: master
- 结果验证
最后
以上就是自信小懒猪为你收集整理的Nginx访问状态以及基于多域名、多端口、多IP配置虚拟主机一、关于Nginx二、Nginx的优化服务三、配置Nginx虚拟主机的全部内容,希望文章能够帮你解决Nginx访问状态以及基于多域名、多端口、多IP配置虚拟主机一、关于Nginx二、Nginx的优化服务三、配置Nginx虚拟主机所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复