Ngnix网站服务
编译安装Nginx服务
1.万恶的防火墙+依赖包安装
[root@localhost /]# cd opt/
[root@localhost opt]# ls
nginx-1.12.0.tar.gz rh
[root@localhost opt]# mount /dev/cdrom /mnt/
[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost opt]# setenforce 0
[root@localhost opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
【ngix的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些开发包,以便提供相应的库和头文件】
2.创建运行用户、组以及编译安装Nginx
- Nginx服务程序默认以nobody身份运行,所以建议为其创建专门的用户账号,以便更准确地控制其访问权限
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
...过程略...
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \ 【指定nginx的安装路径】
> --user=nginx \ 【指定用户名】
> --group=nginx \ 【指定组名】
> --with-http_stub_status_module 【启用http_stub_status_module模块以支持状态统计】
...过程略...
[root@localhost nginx-1.12.0]# make -j2 && make install
...过程略...
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
【让系统识别nginx的操作命令】
3.检查、启动、重启、停止nginx服务
[root@localhost nginx-1.12.0]# 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-1.12.0]# nginx 【启动】
【停止nginx的四种方法】
[root@localhost nginx-1.12.0]# kill -3 4536
[root@localhost nginx-1.12.0]# kill -s QUIT 4658
[root@localhost nginx-1.12.0]# killall -3 nginx
[root@localhost nginx-1.12.0]# killall -s QUIT nginx
【查看nginx的PID号的五种方法】
[root@localhost nginx-1.12.0]# cat /usr/local/nginx/logs/nginx.pid
4722
[root@localhost nginx-1.12.0]# ps -ef | grep nginx
root 4722 1 0 14:52 ? 00:00:00 nginx: master process nginx
nginx 4723 4722 0 14:52 ? 00:00:00 nginx: worker process
root 4763 1723 0 14:55 pts/0 00:00:00 grep --color=auto nginx
[root@localhost nginx-1.12.0]# ss -tnlp | grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=4723,fd=6),("nginx",pid=4722,fd=6))
[root@localhost nginx-1.12.0]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4722 root 6u IPv4 33751 0t0 TCP *:http (LISTEN)
nginx 4723 nginx 6u IPv4 33751 0t0 TCP *:http (LISTEN)
[root@localhost nginx-1.12.0]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4722/nginx: master
【重载四种方法】
[root@localhost nginx-1.12.0]# kill -1 4722
[root@localhost nginx-1.12.0]# kill -s HUP 4722
[root@localhost nginx-1.12.0]# killall -1 nginx
[root@localhost nginx-1.12.0]# killall -s HUP nginx
【日志分隔,重新打开日志文件】
[root@localhost nginx-1.12.0]# kill -USR1 4722
【平滑升级】
[root@localhost nginx-1.12.0]# kill -USR2 4722
4.添加Nginx系统服务
[root@localhost nginx-1.12.0]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.12.2]# systemctl start nginx.service
[root@localhost nginx-1.12.2]# systemctl enable nginx.service
Nginx服务的主配置文件nginx.conf的认知
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
【全局配置】
#user nobody; 【运行用户,若编译时为指定则默认为nobody】
worker_processes 1; 【工作进程数量,可配置成服务器内核数*2.如果网站访问量不大,设为1就足矣】
#error_log logs/error.log; 【错误日志的位置】
#pid logs/nginx.pid; 【PID文件的位置】
【I/O事件配置】
events {
use epoll; 【使用epoll模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能】
worker_connections 1024; 【每个进程处理1024个连接】
}
【如提高每个进程的连接数还需执行"ulimit -n 65535"命令临时修改本地每个进程可以同时打开的最大文件数】
【在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制,因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄】
【ulimit -a命令查看系统允许当前用户进程打开的文件数限制】
【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; 【支持文件发送(下载)】
#tcp_nopush on; 【此选项允许或禁止使用socket的TCP_CORK选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用】
#keepalive_timeout 0;
keepalive_timeout 65; 【连接保持超时时间,单位是秒】
#gzip on; 【gzip模块设置,设置是否开启gzip压缩输出】
【Web服务的监听配置】
server {
listen 80; 【监听地址及端口】
server_name localhost; 【站点域名,当有多个时用空格隔开】
#charset koi8-r; 【网页的默认字符集】
location / { 【根目录配置】
root html; 【网站根目录的位置是/usr/local/nginx/html】
index index.html index.php; 【默认首页文件名】
}
error_page 500 502 503 504 /50x.html; 【内部错误的反馈页面】
location = /50x.html { 【错误页面配置】
root html;
}
日志格式设定
- $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.kgc.com/test/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg
- alias(别名配置):请求www.kgc.com/test/1.jpg,会返回文件/usr/local/nginx/html/1.jpg
- proxy_pass(反向代理配置)
- proxy_pass http://111.0.0.1:8080/
会转发请求到http://111.0.0.1:8080/1.jpg - proxy_pass http://111.0.0.1:8080
会转发请求到http://111.0.0.1:8080/test/1.jpg
- proxy_pass http://111.0.0.1:8080/
访问状态统计配置
1.检查HTTP_STUB_STATUS模块是否安装
[root@localhost ~]# /usr/local/nginx/sbin/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
2.修改nginx.conf配置文件,指定访问位置并添加stub_status配置
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.bak
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf scgi_params.default
fastcgi.conf.default koi-win nginx.conf.bak uwsgi_params
fastcgi_params mime.types nginx.conf.default uwsgi_params.default
fastcgi_params.default mime.types.default scgi_params win-utf
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
35 server {
36 listen 80;
37 server_name www.qzqz.com;
【添加stub_status配置】
47 location /status { 【访问位置为/status】
48 stub_status on; 【打开状态统计功能】
49 access_log off; 【关闭此位置的日志记录】
50 }
[root@localhost conf]# systemctl restart nginx.service
浏览器测试
- 浏览器访问http://192.168.131.14/status
基于授权的访问控制
1.生成用户密码认证文件
[root@localhost /]# yum -y install httpd-tools.x86_64
[root@localhost /]# htpasswd -c /usr/local/nginx/passwd.db qz5
New password:
Re-type new password:
Adding password for user qz5
[root@localhost /]# chown nginx /usr/local/nginx/passwd.db
[root@localhost /]# chmod 400 /usr/local/nginx/passwd.db
2.修改主配置文件相对应目录,添加认证配置项
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
43 location / {
44 root html;
45 index index.html index.php;
【添加认证配置】
46 auth_basic "5514"; 【设置密码提示框文字信息】
47 auth_basic_user_file /usr/local/nginx/passwd.db
48 }
3.重启服务并测试
[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 /]# systemctl restart nginx.service
浏览器访问http://192.168.131.14
基于客户端的访问控制
- 访问控制规则
- deny IP/IP段:拒绝某个IP或IP段的客户端访问
- allow IP/IP段:允许某个IP或IP段的客户端访问
- 规则从上往下执行,如匹配则停止,不再继续往下进行匹配
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
【添加控制规则】
48 deny 192.168.131.13; 【拒绝访问的客户端IP】
49 allow all; 【允许其他IP客户端访问】
[root@localhost /]# systemctl restart nginx.service
IP192.168.131.13主机与其他IP地址主机进行访问测试
基于域名的Nginx虚拟主机
1.为虚拟主机提供域名解析并为主机准备网页文档
[root@localhost /]# echo "192.168.131.14 www.qz.com www.qz1.com" >> /etc/hosts
[root@localhost /]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.131.14 www.qz.com www.qz1.con
[root@localhost /]# mkdir -p /var/www/html/qz
[root@localhost /]# mkdir -p /var/www/html/qz1
[root@localhost /]# echo "this is qz" > /var/www/html/qz/index.html
[root@localhost /]# echo "this is qz1" > /var/www/html/qz1/index.html
2.修改Nginx的配置文件
35 server {
36 listen 80;
37 server_name www.qz.com; 【设置域名www.qz.com】
38
39 charset utf-8;
40
41 access_log logs/www.qz.access.log; 【设置日志名】
43 location / {
44 root /var/www/html/qz; 【设置www.qz.com的工作目录】
45 index index.html index.php;
60 error_page 500 502 503 504 /50x.html;
61 location = /50x.html {
62 root html;
【直接30yy+G到底+p后进行相应修改即可】
126 server {
127 listen 80;
128 server_name www.qz1.com;
130 charset utf-8;
132 access_log logs/www.qz1.access.log;
134 location / {
135 root /var/www/html/qz1;
136 index index.html index.php;
141 }
151 error_page 500 502 503 504 /50x.html;
152 location = /50x.html {
153 root html;
154 }
155 }
156 }
重启服务并测试
[root@localhost /]# systemctl restart nginx.service
- 浏览器访问http://www.qz1.com/
- 浏览器访问http://www.qz.com/
基于IP的Nginx虚拟主机
[root@localhost /]# ifconfig ens33:0 192.168.131.100/24
[root@localhost /]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.131.14 netmask 255.255.255.0 broadcast 192.168.131.255
inet6 fe80::688:d772:e957:2b2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:e4:a1 txqueuelen 1000 (Ethernet)
RX packets 14554 bytes 5585006 (5.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8844 bytes 1256287 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.131.100 netmask 255.255.255.0 broadcast 192.168.131.255
ether 00:0c:29:e4:e4:a1 txqueuelen 1000 (Ethernet)
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
36 listen 192.168.131.14:80; 【设置监听地址192.168.131.14】
127 listen 192.168.131.100:80; 【设置监听地址192.168.50.14】
[root@localhost /]# systemctl restart nginx.service
浏览器访问验证
http://192.168.131.100/
http://192.168.131.14/
基于端口的Nginx虚拟主机
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf
36 listen 192.168.131.14:801;
127 listen 192.168.131.100:808;
[root@localhost /]# systemctl restart nginx.service
浏览器访问验证
http://192.168.131.100:808/
http://192.168.131.14:801/