Linux Operation学习------Nginx

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Operation学习------Nginx相关的知识,希望对你有一定的参考价值。

1、nginx
是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器
1.1认识
Apache,Nginx,Lighttpd(适用于非Java程序写的页面)
Tomcat,Jboss(适用于Java程序写的)
用户概念:源码包安装的时候需要建立一个用户,该用户用来执行该服务(如果没有建立,系统默认使用nobody帐号),以防使用root造成不安全的后果,而yum源在装包的时候会自动为系统建立一个用户。

配置文件:/usr/local/nginx/conf/nginx.conf
日志文件:/usr/local/nginx/logs
网页页面:/usr/local/nginx/html/index.html
自定义网页页面:/usr/local/nginx/目录名称

模块化安装需要的模块
./configure --with-模块名称 --with-模块名称

1.2对原有的nginx添加模块(升级)
1、删除原有的解包后的nginx的目录,重新解压
2、cd到该目录下
3、./configure --with-httpd_ssl_module
4、make (把源码变成二进制程序,多了一个nginx执行程序)
5、不能make install (会覆盖objs原有的文件)
6、cp objs/nginx /usr/local/nginx/sbin #升级只需要拷贝
1.3部署
1)使用源码包安装nginx软件包
[[email protected] ~]# yum –y install gcc pcre-devel openssl-devel //安装常见依赖包 (gcc-c++)
[[email protected] ~]# useradd –s /sbin/nologin nginx
[[email protected] ~]# tar -xf nginx-1.8.0.tar.gz
[[email protected] ~]# cd nginx-1.8.0
[[email protected] nginx-1.8.0]# ./configure \
#> --prefix=/usr/local/nginx \ //指定安装路径
#> --user=nginx \ //指定用户
#> --group=nginx \ //指定组
#> --with-http_ssl_module //开启SSL加密功能
.. ..
make & make install

2)nginx命令的用法 (可以建立软连接,执行方便)
[[email protected] ~]# /usr/local/nginx/sbin/nginx //启动服务
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s stop //关闭服务
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload //重新加载配置文件
[[email protected] ~]# /usr/local/nginx/sbin/nginx –V //查看软件信息
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ (启动服务只需输入nginx)
[[email protected] ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 10441/ngi
2、用户认证
2.1修改Nginx配置文件
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80; #相当于
:80
server_name localhost;
auth_basic "Input Password:"; #认证提示符
auth_basic_user_file "/usr/local/nginx/pass"; #认证密码文件(文件不存在)
location / {
root html;
index index.html index.htm;
}
}
一个server是一个网站
2.2生成密码文件,创建用户及密码
使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools
[[email protected] ~]# yum -y install httpd-tools
[[email protected] ~]# htpasswd -cm /usr/local/nginx/pass tom //创建密码文件(和配置文件内保持一致)
New password:
Re-type new password:
Adding password for user tom
[[email protected] ~]# htpasswd -m /usr/local/nginx/pass jerry
//追加用户,不使用-c选项
New password:
Re-type new password:
Adding password for user jerry

[[email protected] ~]# cat /usr/local/nginx/pass
2.3重启Nginx服务
[[email protected] ~]# /usr/local/nginx/sbin/nginx –s reload

tailf /usr/local/nginx/logs/ #动态查看错误信息

3、基于域名的虚拟主机
3.1服务端:
1)修改Nginx服务配置,添加相关虚拟主机配置如下
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80; //端口
server_name www.aa.com; //域名
auth_basic "Input Password:"; //认证提示符
auth_basic_user_file "/usr/local/nginx/pass"; //认证密码文件
location / {
root html; //指定网站根路径
index index.html index.htm;
}
}
server {
listen 80; //端口
server_name www.bb.com; //域名
location / {
root www; //指定网站根路径(需要创建目录名字为www)
index index.html index.htm;
}
}
在文件中使用ctrl+v并移动上下键可以选定字符,按x可以删除
2)创建网站根目录及对应首页文件
[[email protected] ~]# mkdir /usr/local/nginx/www
[[email protected] ~]# echo "www" > /usr/local/nginx/www/index.html
3)重启nginx服务
[[email protected] ~]# /usr/local/nginx/sbin/nginx –s reload
3.2客户端:
1)修改客户端主机192.168.4.100的/etc/hosts文件,进行域名解析
/etc/hosts #本地域名解析文件 在客户端上改 优先级高于DNS
[[email protected] ~]# vim /etc/hosts
192.168.4.5 www.aa.com www.bb.com
2)测试 firefox http://www.aa.com firefox http://www.bb.com
4、SSL虚拟主机
加密算法:
(1)对称加密
(2)非对称加密
(3)哈希值 md5sum +文件
4.1生成私钥与证书
[[email protected] ~]# cd /usr/local/nginx/conf
[[email protected] ~]# openssl genrsa -out cert.key (或者openssl genrsa > cert.key) //生成私钥
[[email protected] ~]# openssl req -new -x509 -key cert.key -out cert.pem //生成证书
国家,省份,城市,公司,部门,主机名
4.2修改Nginx配置文件,设置加密网站的虚拟主机
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 443 ssl;
server_name www.cc.com;
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; #此处修改需要在/usr/local/nginx/下新建名称一致的目录名
index index.html index.htm;
}
}
ngnix -s reload #重新加载
4.3客户端验证
[[email protected] ~]# vim /etc/hosts
192.168.4.5 www.cc.com www.aa.com www.bb.com
[[email protected] ~]# firefox https://www.cc.com //信任证书后可以访问
5、Nginx反向代理
负载均衡;检查后台情况(web高可用);
5.1部署后端Web服务器
yum -y install httpd
echo "192.168.2.100" > /var/www/html/index.html
systemctl restart httpd
5.2部署Nginx服务器
由于配置文件在使用过程中,在/usr/local/nginx/conf下存在自带配置备份文件
nginx.conf.default
在实际工作中修改配置文件前对其进行先备份
cp nginx.conf.default nginx.conf (对于本实验覆盖后重新做)
修改/usr/local/nginx/conf/nginx.conf配置文件
http {
upstream webserver { #定义一个web集群,取名webserver
server 192.168.2.100:80; #后台服务器
server 192.168.2.200:80;
} #该函数定义集群
#可以添加weight权重:调用次数 ,
#max_fails失败次数:允许最多连接后台web失败的次数
#fail_timeout=10 超时时间:当失败后,10s后再询问后台的web是否正常(加down10s后不在询问)
server {
listen 80;
server_name www.tarena.com;
location / { #匹配用户的地址栏
proxy_pass http://webserver; #调用集群
root html; #该路径不再寻找
5.3重启服务
/usr/local/nginx/sbin/nginx –s reload
客户端轮询访问2个web

5.4设置相同客户端访问相同Web服务器
upstream webserver { #定义一个web集群,取名webserver
ip_hash; #客户端第一次访问之后,以后继续分配到这台web
server 192.168.2.100:80; #后台服务器
server 192.168.2.200:80;
}
客户端访问只访问一个web


1、动态网站
LNMP(Linux,nginx,mariadb,php,python)
1.1安装nginx
1)解包 tar -xf
配置 ./configure --prefix=/usr/local/nginx --with-http_ssl_module
编译 make
安装 make install
2)mariadb 使用mysql命令
mariadb-server 存放数据的地方 监听3306端口
mariadb-devel 依赖关系
3)php【解释器】
<?php #开头 内容 结尾 ?>
php-fpm 监听9000端口 自动解释代码的服务
php-mysql 扩展包,连接数据库
4)启动服务以及查看端口状态
/usr/local/nginx/sbin/nginx #启动Nginx服务
netstat -utnlp | grep :80
systemctl start mariadb #启动数据库服务
netstat -utnlp | grep :3306 或者 systemctl status mariadb
systemctl start php-fpm #启动php-fpm服务
netstat -utnlp | grep :9000 或者 systemctl status php-fpm

1.2动静分离
当用户访问网站时会根据location来匹配寻找的页面,没有匹配的就匹配 / 中的内容
需要将php脚本放在/usr/local/nginx/html下,并修改下面的配置文件
vim /usr/local/nginx/conf/nginx.conf
location ~ .php$ { #匹配是否以.php结尾
root html; #页面的位置
fastcgi_pass 127.0.0.1:9000; #把找到的页面给了9000(php-fpm IP与端口)
fastcgi_index index.php;
include fastcgi.conf; #加载fastcgi.conf参数文件
}
日志查看:tailf /usr/local/nginx/logs/error.log
ls /var/log/php-fpm/error.log

测试:firefox http://192.168.4.5/1.php

FastCGI :是一种常驻(long-live)型的CGI
将CGI解释器进程保持在内存中,进行维护与调度
配置文件路径:/etc/php-fpm.d/www.conf

2、地址重写
获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程
rewrite 旧链接(支持正则) 新链接 [选项];
选项:last 不再读其他rewrite;break 不再读其他语句,结束;
redirect 让地址栏变化,用户能看到url的改变(临时);permament让地址栏变化(永久)
1)修改配置文件(访问a.html重定向到b.html)
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
rewrite /a.html /b.html; #访问a.html的内容会跳到b.html (可以缩短URL)
}
}
echo www.a.com > html/a.html
echo www.b.com > html/b.html
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] ~]# firefox http://192.168.4.5/a.html
2)修改配置文件(访问192.168.4.5的请求重定向至www.a.com)
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
rewrite ^/ http://www.a.com/; #在进入网站之前匹配到以 / 开头的都跳转到该网站
location / {
root html;
index index.html index.htm;
}
}
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
3)修改配置文件(访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面)
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
rewrite ^/(.) http://www.a.com/$1; #访问子网站都跳转到现有对应的,一个()对应一个$
location / {
root html;
index index.html index.htm;
}
}
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
访问http://192.168.4.5/web/login_new.html 跳转到
http://www.tmooc.cn/web/login_new.html
4)实现curl和火狐访问相同连接返回的页面不同
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
if ($http_user_agent ~
url) { //识别客户端curl浏览器 不分大小写
rewrite ^(.
)$ /curl/$1 break;
}
}
[[email protected] ~]# echo "firefox" > /usr/local/nginx/html/test.html
[[email protected] ~]# mkdir -p /usr/local/nginx/html/curl/
[[email protected] ~]# echo "curl" > /usr/local/nginx/html/curl/test.html
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] ~]# firefox http://192.168.4.5/test.html #出现firefox页面
[[email protected] ~]# curl http://192.168.4.5/test.html #返回curl的信息
实际应用:区分是PC页面或者手机页面或者其他
$http_user_agent 用户请求的包含用户的信息的变量
tailf /usr/local/nginx/logs/access.log #访问信息日志
192.168.4.254 - - [07/Jan/2018:21:48:16 -0500] "GET /test.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
192.168.4.254 - - [07/Jan/2018:21:48:16 -0500] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
访问服务器的用户IP - 用户名 时间 访问什么 用什么系统访问 用什么浏览器访问

[[email protected] ~]# curl -A firefox http://192.168.4.5/test.html
Firefox #并不显示curl
#curl伪装成firefox进行对服务器访问,服务器访问日志显示伪装后的信息


1、Nginx常见问题处理
1.1不显示Nginx软件版本号
server_tokens off/on; #服务器版本号信息
1.2并发量
ab –n 2000 –c 2000 http://192.168.4.5/ -c 并发量 -n 请求数
如何增大并发量的容量
1)通过nginx配置文件修改
worker_processes 2; #与CPU核心数量一致
events {
worker_connections 65535; #每个worker最大并发连接数
use epoll;
}
2)通过Linux系统内核
ulimit -a #查看最大所有限制
ulimit –Hn 100000 #临时有效
ulimit –Sn 100000 #临时有效
-S软限制 (用户可以修改) n(最大文件数量)
-H硬限制 (用户不可以修改) n(最大文件数量)
ss -anptu | grep nginx #实时查看并发量有多少人访问 (| wc -l)
永久设置:
[[email protected] ~]# vim /etc/security/limits.conf
<domain> <type> <item> <value>

  • soft nofile 100000
  • hard nofile 100000
    <type>只能是soft或者hard
    1.3如何解决客户端访问头部信息过长的问题
    报错信息414(缓存不够)<head><title>414 Request-URI Too Large</title></head>
    [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
    http {
    server_tokens off; //不显示nginx版本号信息
    client_header_buffer_size 1k; //默认请求包头信息的缓存
    large_client_header_buffers 4 4k; //请求包头部信息的缓存个数与容量
    ......
    1.4开启gzip压缩功能,提高数据传输效率
    [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
    gzip on; #开启压缩功能
    gzip_min_length 1000; #字节限定(小文件不压缩)
    gzip_comp_level 4; #压缩比率1-9
    gzip_types text/plain #对什么格式的文件压缩
    参考:/usr/local/nginx/conf/mime.types 将格式左边的写到gzip_types后
    对mp4,mp3,jpg不能压缩,多媒体文件基本都是压缩格式
    1.5如何让客户端浏览器缓存数据
    about:cache #查看浏览器缓存
    针对不变的数据进行缓存
    [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
    server {
    listen 80;
    server_name www.tarena.com;
    location / {
    root html;
    index index.html index.htm;
    }
    location ~* .(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    expires 30d; #定义客户端缓存时间为30天
    }
    }
    1.6如何自定义返回给客户端的404错误页面
    server {
    ....
    charset utf-8;
    error_page 404 /40x.html; //自定义错误页面
    location = /40x.html {
    root html;
    }
    }

/usr/local/nginx/conf/sbin/nginx -s reload

以上是关于Linux Operation学习------Nginx的主要内容,如果未能解决你的问题,请参考以下文章

Linux Operation学习------Nginx

Linux Operation学习------Nginx

Linux Operation学习------Squid/Varnish

Linux Operation学习------SVN/RPM打包

ng add ng-zorro-antd 安装时报错 已经是管理员还需要权限Error: EPERM: operation not permitted, lstat 'C: gWorksp(

linux学习记录-----vsftpd文件上传(550 create directory operation failed)