大佬教程收集整理的这篇文章主要介绍了Linux Nginx网站服务优化配置| 授权访问控制 | 访问状态统计 | 超详细,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
cd /opt
Nginx-1.12.0.tar.gz
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
useradd -M -s /sbin/nologin Nginx
cd /opt
tar zxvf Nginx-1.12.0.tar.gz
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号> #平滑升级
方法一:添加/etc/init.d/Nginxetc/init.d/Nginx
vim /etc/init.d/Nginx
#!/bin/bash
#chkconfig: 35 99 20 #运行级别3、5 第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 "please use: $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 #开启服务
方法二:添加 /lib/systemd/system/Nginx.service
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 #开启自启动服务
注意:Nginx服务安装具体步骤看我之间的博客,此处不再贴安装过程
vim /usr/local/Nginx/conf/Nginx.conf
注意:整个配置文件大约117行
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量,可配置成服务器内核数 * 2
#error_log logs/error.log; #错误日志文件的位置
#pid logs/Nginx.pid; #PID 文件的位置
events {
use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 1024; #每个进程处理 1024 个连接
}
`#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.`
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;
##此选项允许或禁止使用socke的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name www.accp.com;
##网页的默认字符集
charset utf-8;
##根目录配置
LOCATIOn / {
##网站根目录的位置/usr/local/Nginx/html
root html;
##默认首页文件名
index index.html index.htm;
}
##内部错误的反馈页面
error_page 500 502 503 504 /50x.html;
##错误页面配置
LOCATIOn = /50x.html {
root html;
}
}
}
### ➤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地址了,通过$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forWARDed_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
LOCATIOn常见配置指令,root、alias、proxy_pass
root(根路径配置):请求www.accp.com/test/1.jpg,会返回文件/usr/local/Nginx/html/test/1.jpg
alias(别名配置):请求www.accp.com/test/1.jpg,会返回文件/usr/local/Nginx/html/1.jpg
先使用命令/usr/local/Nginx/sbin/Nginx -V 查看已安装的 Nginx 是否包含 http_stuB_STATUS 模块
指定访问位置并添加 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.accp.com;
charset utf-8;
LOCATIOn / {
root html;
index index.html index.php;
}
##添加 stub_status 配置##
LOCATIOn /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
systemctl restart Nginx
浏览器访问 http://192.168.78.11/status
Active connections :表示当前的活动连接数;
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。
yum install -y httpd-tools
htpasswd -c /usr/local/Nginx/user zhangsan
chown Nginx /usr/local/Nginx/user
chmod 400 /usr/local/Nginx/user
vim /usr/local/Nginx/conf/Nginx.conf
......
server {
LOCATIOn / {
......
##添加认证配置##
auth_basic "Hello world";
auth_basic_user_file /usr/local/Nginx/user;
}
}
Nginx -t
systemctl restart Nginx
#浏览器访问 http://192.168.78.11/index.html
访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。
vim /usr/local/Nginx/conf/Nginx.conf
......
server {
LOCATIOn / {
......
##添加控制规则##
deny 192.168.78.22; #拒绝访问的客户端 IP
allow all; #允许其它IP客户端访问
}
}
systemctl restart Nginx
echo "192.168.78.11 www.accp.com " >> /etc/hosts
echo "192.168.78.11 www.benet.com " >> /etc/hosts
@H_626_111@mkdir -p /var/www/html/accp mkdir -p /var/www/html/benet echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
vim /usr/local/Nginx/conf/Nginx.conf
......
http {
......
server {
listen 80;
server_name www.accp.com; #设置域名www.accp.com
charset utf-8;
access_log logs/www.accp.access.log;
LOCATIOn / {
root /var/www/html/accp; #设置www.accp.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.benet.com; #设置域名www.benet.com
charset utf-8;
access_log logs/www.benet.access.log;
LOCATIOn / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
LOCATIOn = 50x.html{
root html;
}
}
}
systemctl restart Nginx
浏览器访问
http://www.accp.com
http://www.benet.com
增加临时虚拟网卡
ifconfig ens33:0 192.168.78.111 netmask 255.255.255.0
vim /usr/local/Nginx/conf/Nginx.conf
......
http {
......
server {
listen 192.168.184.78.11:80; #设置监听地址
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
LOCATIOn / {
root /var/www/html/accp;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
LOCATIOn = 50x.html{
root html;
}
}
server {
listen 192.168.78.111:80; #设置监听地址
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
LOCATIOn / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
LOCATIOn = 50x.html{
root html;
}
}
}
systemctl restart Nginx
@H_336_404@
浏览器访问
http://192.168.78.11
http://192.168.78.111
@H_162_419@
vim /usr/local/Nginx/conf/Nginx.conf
......
http {
......
server {
listen 192.168.78.11:8080; #设置监听 8080 端口
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
LOCATIOn / {
root /var/www/html/accp;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
LOCATIOn = 50x.html{
root html;
}
}
server {
listen 192.168.78.11:9090; #设置监听 9090 端口
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
LOCATIOn / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
LOCATIOn = 50x.html{
root html;
}
}
}
systemctl restart Nginx
浏览器访问
http://192.168.78.11:8080
http://192.168.78.11:9090
以上是大佬教程为你收集整理的Linux Nginx网站服务优化配置| 授权访问控制 | 访问状态统计 | 超详细全部内容,希望文章能够帮你解决Linux Nginx网站服务优化配置| 授权访问控制 | 访问状态统计 | 超详细所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。