linux 运维 nginx服务器

nginx(web服务器)

nginx是一个高性能的http和反向代理服务器,
同时也是一个imap/pop3/smtp 代理服务器
比apache简单
官网:http://nginx.org

nginx配置文件及目录
/usr/local/nginx(安装目录)
/usr/local/nginx/conf/nginx.conf(主配置文件)
/usr/local/nginx/html(网页目录)
/usr/local/nginx/logs(日志文件,pid)
/usr/local/nginx/sbin/nginx(启动脚本)

nginx安装
[root@daili ~]# useradd nginx
[root@daili ~]# tar -zxvf nginx-1.7.10.tar.gz
[root@daili ~]# cd nginx-1.7.10
[root@daili nginx-1.7.10~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@daili nginx-1.7.10~]# make
[root@daili nginx-1.7.10~]# make install
[root@daili ~]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@daili ~]# cd /usr/local/nginx/sbin/
nginx(启动)
nginx -s stop(关闭)
nginx -s reload(重启)
[root@beiqiang ~]# fierfox http://192.168.4.5(测试访问nginx)

nginx选项
常用选项
-v:查看nginx版本
-V:查看编译参数
-t:测试默认配置文件
-c:指定配置文件
[root@daili nginx]# nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

nginx进程管理
显示进程:
ps aux |grep 进程名

停止nginx:
pkill/kill -信号 进程名/pid号
pkill -int nginx

常见信号:
HUP:重新配置文件
TERM,INT:快速关闭
QUIT:关闭主进程及子进程
USR1:重新打开日志文件
USR2:平滑升级可执行程序

案列:在不停止服务的情况下升级软件版本
[root@daili ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@daili ~]# cd nginx-1.8.0
[root@daili nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@daili nginx-1.8.0]# make
[root@daili ~]# cd /usr/cocal/nginx/sbin
[root@daili sbin~]# mv nginx nginxold (改名旧脚本)
[root@daili ~]# cd /root/nginx-1.0.5/objs/
[root@daili objs~]# cp nginx /usr/local/nginx/sbin(拷贝新脚本)
[root@daili nginx-1.8.0~]# make upgrade (升级软件)
。。。。
[root@daili sbin~]# ./nginx -v(查看版本)

——————————————————————————————————————————————————————————
nginx配置解析

全局选项:
user nginx:进程所有者
worker_processes 1;(启动进程数)
error_log /var/log/nginx/error.log(日志文件)
pid /var/run/nginx.pid(pid文件)
events {(单个进程最大并发量)
worker_connections 1024;

}

[root@daili ~]# vim /usr/local/nginx/conf/nginx.conf
http {
server {(定义虚拟主机)
listen 80;
server_name localhost;
location / {(发布目录)
root html;(网站根路径)
index index.html index.htm;
}
}

}

用户认证及访问控制
location / {(发布目录)
root html;(网站根路径)
index index.html index.htm;
allow 192.168.8.1;(只允许192.168.8.1访问)
deny all;
auth_basic "提示";(验证用户)
auth_basic_user_file /usr/local/nginx/conf/pass;(存用户文件密码)
}
[root@daili nginx]# yum -y install httpd-tools(安装生成密码的程序)
[root@daili ~]# htpasswd -cm /usr/local/nginx/conf/pass admin(创建认证用户,m选项加密)
[root@daili nginx]# cat /usr/local/nginx/conf/pass (查看用户名,密码)
admin:$apr1$H7ZU5sxb$wiKhvcV/g7jZYxEBlo5jh.
[root@daili ~]# nginx -s reload(重启)

实验:
访问控制和用户认证

访问控制:
[root@daili ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 192.168.4.1;
deny all;
}
[root@daili nginx]# nginx -s
[root@daili ~]# nginx -s reload(重启)
[root@beiqiang ~]# curl http://192.168.4.5(4.1ip测试访问成功)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

[root@room1pc01]# curl http://192.168.4.5(其他主机访问失败)
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

用户认证:
location / {
root html;
index index.html index.htm;
auth_basic "hail hydra!";
auth_basic_user_file /usr/local/nginx/conf/pass;
}
[root@daili nginx]# yum -y install httpd-tools(安装生成密码的程序)
[root@daili ~]# htpasswd -cm /usr/local/nginx/conf/pass admin(创建认证用户,m选项加密)
[root@daili nginx]# cat /usr/local/nginx/conf/pass (查看用户名,密码)
hydra:$apr1$H7ZU5sxb$wiKhvcV/g7jZYxEBlo5jh.
[root@daili ~]# nginx -s reload(重启后测试访问)

综合测试:
部署nginx web服务器
所有人访问页面都需要认证
网站根目录下所有主机都可以访问
为网站创建一个二级目录/test,并生成index.html且这目录仅192.168.4.254可以访问
[root@daili nginx]# echo hydra > html/test/index.html
[root@daili test]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
auth_basic "hail hydra!";
auth_basic_user_file /usr/local/nginx/conf/pass;
}
location /test {
allow 192.168.4.254;
deny all;
}
[root@daili nginx]# nginx -s reload(重启后测试)

——————————————————————————————————————————————————

虚拟主机

基于域名,基于端口,基于ip

基于域名的虚拟主机:
server {(第一个虚拟主机)
listen 80 default;(默认站点default)
server_name www.xx.com;(xx域名)
location / {
root html;(网页)
index index.html index.htm;
auth_basic "hail hydra!";
auth_basic_user_file /usr/local/nginx/conf/pass;
}
location /test {
allow 192.168.4.254;
deny all;
}

server {(第二个虚拟主机)
listen 80;
server_name www.xxx.com;(xxx域名)

location / {
root web;(去创建一个网页)
index index.html index.htm;
}
}
[root@daili nginx]# ls
html ........ web
[root@daili nginx]# nginx -s reload(重启测试)

基于端口的虚拟主机:
server {
listen 8080;端口
server_name www.xx.com;域名
location / {
root html;
index index.html index.htm;
auth_basic "hail hydra!";
auth_basic_user_file /usr/local/nginx/conf/pass;
}
location /test {
allow 192.168.4.254;
deny all;
}

server {
listen 8000;端口
server_name www.xx.com;域名

location / {
root web;
index index.html index.htm;
}
}
[root@daili nginx]# ls
[root@daili nginx]# nginx -s reload


基于ip的虚拟主机
server {(第一个虚拟主机)
listen 192.168.1.1:80;ip,端口
server_name www.xx.com; 域名
location / {
root html;
index index.html index.htm;
auth_basic "hail hydra!";
auth_basic_user_file /usr/local/nginx/conf/pass;
}
location /test {
allow 192.168.4.254;
deny all;
}

server {(第二个虚拟主机)
listen 192.168.1.2:80;ip,端口
server_name www.xx.com; 域名

location / {
root web;
index index.html index.htm;
}
}
[root@daili nginx]# nginx -s reload

——————————————————————————————————————————————————
ssl虚拟主机

算法:
对称加密:aes,des
非对称加密:rsa,dsa
信息摘要:md5,sha256

ssl加密网站的核心技术是非对称生成密钥
公钥,私钥,证书

[root@daili ]# cd /usr/local/nginx/conf/(到配置文件目录下)
[root@daili conf]# openssl genrsa -out my.key(生成私钥)
[root@daili conf]# openssl req -new -x509 -key my.key -out my.crt(生成自签名证书,依次填入信息)
-----
Country Name (2 letter code) [XX]:CN(国家)
State or Province Name (full name) []:beijing(省份)
Locality Name (eg, city) [Default City]:beijing(城市)
Organization Name (eg, company) [Default Company Ltd]:Anonymous(公司)
Organizational Unit Name (eg, section) []:Anonymous(部门)
Common Name (eg, your name or your server's hostname) []:test(注释)
Email Address []:Anonymous@Anonymous.com (邮箱)
[root@daili conf]# vim nginx.conf
server {(加密虚拟主机)
listen 443 ssl;(默认监听端口)
server_name www.xxxx.com;(加密虚拟主机)
ssl on; (开启ssl)
ssl_certificate my.crt;(指定证书文件)
ssl_certificate_key my.key;(指定私钥文件)

ssl_session_cache shared:SSL:1m;(缓存时间一分钟)
ssl_session_timeout 5m;(超时时间5分钟)

ssl_ciphers HIGH:!aNULL:!MD5;(除了md5加密 其他算法都支持,因为md5被破解了)
ssl_prefer_server_ciphers on;

location / {
root web2;(页面)
index index.html index.htm;
}
}
[root@beiqiang ~]# firefox https://www.xxxx.com(测试访问)

——————————————————————————————————————————————————

nginx负载分配方式:
轮询(默认的):逐一循环
weight:指定轮询几率,权重和访问比率成正比
ip_hash:根据客户端ip分配固定的后端服务器
fair:按后端服务器响应时间短的优先分配

服务器主机状态:
类型
down:表示当前server暂时不参与负载
max_fails:允许请求失败(默认为1)
fail_timeout:max_fails次失败后,暂停提供服务的时间
backup:备份服务器

nginx测试:
beiqiang:192.168.4.1 eth0
daili(装了nginx服务器): 192.168.4.5 eth0
相当于调度器 192.168.2.5 eth1
web: 192.168.2.100 eth1(内容不一样)
web2: 192.168.2.200 eth1(内容不一样)

两台web都安装了httpd,启动服务,配置页面,用调度器(主机adili)测试访问
[root@daili conf]# vim nginx.conf
http{
upstream hydra{(定义源服务器组)
server 192.168.2.100 max_fails=1 fail_timeout=30;(web主机)
server 192.168.2.200 max_fails=1 fail_timeout=30;(web2主机)
}
server {
listen 80;
server_name www.xx.com;
location / {
proxy_pass http://hydra;(当有人访问xx.com时,调用hydra集群)
root html;
index index.html index.htm;
}
}
}

————————————————————————————————————————————————————————

原文地址:https://www.cnblogs.com/Hydraxx/p/7371595.html