目录
3、Nginx.conf介绍(/usr/local/nginx/conf/nginx.conf)
1、基于授权的访问控制 htpasswd是一个用于目录访问权限认证的一个工具
引言
今天给大家介绍一个目前很多公司都在用的前端页面的网址服务,是目前应用最多的服务之一,在过去我们用的比较多的是Apache,现在是Nginx,并不是说现在没有人用Apache,而是更具具体的公司环境,现在Nginx更适合大多数的公司,被广泛应用的原因也是因为它有自己独特的优点
一、基础服务
1、Nginx优点
—款高性能、轻量级Web服务软件
稳定性高,系统资源消耗低,对HTTP并发连接的处理能力高(单台物理服务器可支持30000~50 000个并发请求,具体根据实际环境情况,大约在3万)
2、编译安装
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
cd /opt
rz -E
nginx-1.12.2.tar.gz
安装依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
创建运行用户、组
useradd -M -s /sbin/nologin nginx
编译安装Nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \ #指定nginx的安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定组名
--with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #让系统识别nginx的操作命令
nginx -t #检查配置文件是否配置正确
nginx #启动
cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
kill -3 <PID号>
kill -s QUIT <PID号> #停止
killall -3 nginx
killall -s QUIT nginx
kill -1 <PID号> #重载
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx #日志分隔,重新打开日志文件
kill -USR1 <PID号> #平滑升级
kill -USR2 <PID号>
添加Nginx系统服务
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 #赋权,除了root以外的用户都不能修改
systemctl start nginx.service
systemctl enable nginx.service
或者
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20 # chkcofig - “-” 表示不启用开机启动管理
(同时 若不加“#”, chkconfig add nginx 会加载不到配置)
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx" #命令程序文件位置(nginx)
PID="/usr/local/nginx/logs/nginx.pid" #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
3、Nginx.conf介绍(/usr/local/nginx/conf/nginx.conf)
#user nobody; #全局配置
worker_processes 1; #最大工作进程
#error_log logs/error.log; #工作日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #pid位置
events { #i/o事件配置
worker_connections 1024; #进程最大可连接数
}
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;
#keepalive_timeout 0; #超时时间
keepalive_timeout 65;
#gzip on;
server { #服务定义
listen 80; #监控端口
server_name localhost; #服务名
#charset koi8-r; #字符语言
#access_log logs/host.access.log main;
location / { #正则表达式,匹配的是/usr/local/nginx/html/index.html 50x.html
root html;
index index.html index.htm;
}
#error_page 404 /404.html; #错误页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #匹配50x.html
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以php结尾的php(\转义)
# proxy_pass http://127.0.0.1; #跳转页面
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #php端口
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params; #变量文件
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configurati
on #
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server #加密方式
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
二、访问控制
1、基于授权的访问控制 htpasswd是一个用于目录访问权限认证的一个工具
-c:创建密码文件,如果文件存在,那么内容被清空中写
生成用户密码认证文件,并添加Nginx服务,给400权限
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhang
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db
修改主配置文件相对应目录。添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
location / { ##添加认证配置##
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
root html;
index index.html index.htm;}
}
重启服务
nginx -t
systemctl restart nginx
总结
前面也和大家说了现在被广泛使用的是Nginx,下面就说一说它的优点
1、httpd和nginx区别,首先nginx与httpd以静态页面处理+动态页面转发的功能上比较类似,但是nginx优势在于抗高并、发轻量级、性能稳定
2、nginx的配置文件中包含的模块从全局,具体匹配的URI,分为以下几种
全局模块——在此模块中定义的内容,会生效于所有配置
http——应用于用户以http方式访问nginx这个过程
server服务——适用于通过端口、ip 、域名的访问方式的请求
location——URL www .ly.com/ index.html
3、NG并发连接能力影响因素
cup个数和本地物理服务器系统的最大文件打开数