社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

详解 nginx 配置文件及构建 web 虚拟主机

小白的成功进阶之路 • 4 年前 • 249 次点击  

一、添加为系统服务

1、方法一

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

2、方法二

vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $COM
;;

stop)
  kill -s QUIT $(cat $PID)
;;

restart)
  $0 stop
  $0 start
;;

reload)
  kill -s HUP $(cat $PID)
;;

*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1

esac
exit 0

chmod +x /etc/init.


    
d/nginx
chkconfig --add nginx							#添加为系统服务
systemctl stop nginx
systemctl start nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

在这里插入图片描述

在这里插入图片描述

二、nginx 主配置文件 nginx.conf

vim /usr/local/nginx/conf/nginx.conf 
  • 1
  • 1

1、全局配置

#user nobody; 					#运行用户,若编译时未指定则默认为 nobody
worker_processes 1; 			#工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了
#error_log logs/error.log; 		#错误日志文件的位置
#pid logs/nginx.pid; 			#PID 文件的位置
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

2、I/O 事件配置

events {
    use epoll; 					#使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
    worker_connections 4096; 	#每个进程处理 4096 个连接,系统默认为1024,可更改
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
  • 在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
  • 可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。

在这里插入图片描述

  • 查看内核和CPU

在这里插入图片描述

3、HTTP 配置

http {
	##文件扩展名与文件类型映射表
    include       mime.types;
	##默认文件类型
    default_type  application/octet-stream;
	##日志格式设定
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
	##访问日志位置
    #access_log  logs/access.log  main;
	##支持文件发送


    
(下载)
    sendfile        on;
	##此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;
	##连接保持超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
	##gzip模块设置,设置是否开启gzip压缩输出
    #gzip  on;
	
	##Web 服务的监听配置
	server {
		##监听地址及端口
		listen 80; 
		##站点域名,可以有多个,用空格隔开
		server_name www.ljm.com;
		##网页的默认字符集
		charset utf-8;
		##根目录配置
		location / {
			##网站根目录的位置/usr/local/nginx/html
			root html;
			##默认首页文件名
			index index.html index.php;
		}
		##内部错误的反馈页面
		error_page 500 502 503 504 /50x.html;
		##错误页面配置
		location = /50x.html {
			root html;
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

在这里插入图片描述

在这里插入图片描述

4、日志格式设定

$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

location常见配置指令,root、alias、proxy_pass
root(根路径配置):请求www.ljm.com/test/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg
alias(别名配置):请求www.ljm.com/test/1.jpg,会返回文件/usr/local/nginx/html/1.jpg
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

5、访问状态统计配置

1、先使用命令/usr/local/


    
nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
2、修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ljm.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##添加 stub_status 配置##
		location /status { 					#访问位置为/status
			stub_status on; 				#打开状态统计功能
			access_log off; 				#关闭此位置的日志记录
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

在这里插入图片描述

6、基于授权的访问控制

1、生成用户密码认证文件
yum -y install httpd-tools
htpasswd -c /usr/local/nginx/passwd.db qiaodaer
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db 
#修改权限的话需要修改属主,因为文件属主为root,而运行nginx的账号为nginx


2、修改主配置文件相对应目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加认证配置##
			auth_basic "secret";
			auth_basic_user_file /usr/local/nginx/passwd.db;
		}
	}


3、重启服务,访问测试
nginx -t
systemctl restart nginx

浏览器访问 http://192.168.184.70或www.ljm.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

7、基于客户端的访问控制

访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。

vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加控制规则##
			deny 192.168.184.31; 					#拒绝访问的客户端 IP
			allow all;								#允许其它IP客户端访问
		}
	}

systemctl restart nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

三、构建 Nginx 虚拟 web 主机

1、基于域名的虚拟主机

①、为虚拟主机提供域名解析

方法一:部署DNS域名解析服务器,来提供域名解析(脚本内容)

#!/bin/bash
mount /dev/sr0 /mnt
yum -y install bind &> /dev/null

#修改主配置文件 :/etc/named.conf
sed -i 's/127.0.0.1;/any;/' /etc/named.conf
sed -i 's/localhost;/any;/' /etc/named.conf

for ((;;))
do

read -p "请输入你需要配置的域名(例www.abc.com):" a
b=`echo $a | awk -F "." 'BEGIN{OFS="."}{$2=$2;print$2,$3}'`
c=`ip a | grep "ens33" | awk NR==2'{print}' | awk -F/ '{print$1}' | awk '{print$2}'`

#修改区域配置文件 :/etc/named.rfc1912.zones

echo "zone \"$b\" IN {
        type master;
        file \"$b.zone\";
        allow-update { none; };
};" >> /etc/named.rfc1912.zones


#修改区域数据配置文件 :/var/named/named.localhost
cd /var/named
cp 


    
-p named.localhost $b.zone

sed -i "2c @       IN SOA  $b. rname.invalid. (" /var/named/$b.zone
sed -i "8c NS  $b." /var/named/$b.zone && sed -i "8 s/^/\t/" /var/named/$b.zone
sed -i "9c   A  $c" /var/named/$b.zone && sed -i "9 s/^/\t/" /var/named/$b.zone
sed -i "10c www IN A $c" /var/named/$b.zone


#添加指定dns服务器
sed -i "2c nameserver $c" /etc/resolv.conf

read -p "是否需要继续添加(y/n):" d
case $d in
y)
continue
;;

n)

#关闭系统防火墙和系统安全机制
systemctl stop firewalld
setenforce 0

#开启dns服务
systemctl restart named
break
;;
*)
echo "请正确输入"
systemctl stop firewalld
setenforce 0
systemctl restart named
break
esac
done
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

方法二:在 /etc/hosts 文件中临时配置域名与IP地址的映射

echo "192.168.184.70 www.wangdaer.com www.qiaodaer.com" >> /etc/hosts
  • 1
  • 1

在这里插入图片描述

②、为虚拟主机准备网页文档

mkdir -p /var/www/


    
html/qiaodaer
mkdir -p /var/www/html/wangdaer
echo "<h1>www.qiaodaer.com</h1>" > /var/www/html/qiaodaer/index.html
echo "<h1>www.wangdaer.com</h1>" > /var/www/html/wangdaer/index.html
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

③、添加虚拟主机配置

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.qiaodaer.com;					#设置域名www.qiaodaer.com
		charset utf-8;
		access_log logs/www.qiaodaer.access.log; 
		location / {
			root /var/www/html/qiaodaer;				#设置www.qiaodaer.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 80;
		server_name www.wangdaer.com;					#设置域名www.wangdaer.com
		charset utf-8;
		access_log logs/www.wangdaer.access.log; 
		location / {
			root /var/www/html/wangdaer;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

在这里插入图片描述

④、重启服务并进行测试

在这里插入图片描述
在这里插入图片描述

2、基于 IP地址 的虚拟主机

ifconfig ens33:0 192.168.184.31 netmask 255.255.255.0 

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
		listen 192.168.184.70:80;					#设置监听地址
		server_name www.wangdaer.com;
		charset utf-8;
		access_log logs/www.wangdaer.access.log; 
		location / {
			root /var/www/html/wangdaer;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
server {
	listen 192.168.184.170:80;					#设置监听地址
	server_name www.qiaodaer.com;
	charset utf-8;
	access_log logs/www.qiaodaer.access.log; 
	location / {
		root /var/www/html/qiaodaer;
		index index.html index.php;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}	
}

systemctl restart nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

3、基于端口的虚拟主机

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.184.70:666;					#设置监听 666 端口
		server_name www.qiaodaer.com;
		charset utf-8;
		access_log logs/www.qiaodaer.access.log; 
		location / {
			root /var/www/html/qiaodaer;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}

server {
	listen 192.168.184.70:888;					#设置监听 888 端口
	server_name www.wangdaer.com;
	charset utf-8;
	access_log logs/www.wangdaer.access.log; 
	location / {
		root /var/www/html/wangdaer;
		index index.html index.php;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}

systemctl restart nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/110766
 
249 次点击