Nginx配置实战

简介:

一、基于域名虚拟主机配置

1、修改nginx.conf配置文件

[root@linux-node2 ~]# cat /etc/nginx/nginx.conf
worker_processes  auto;
error_log /var/log/nginx/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
   server {
    listen 80;
    server_name bbs.abc.org;
    root html/bbs;
    index index.html index.htm;
    access_log /var/log/nginx/bbs_access.log main;
    }
   server {
    listen 80;
    server_name blog.abc.org;
    root html/blog;
    index index.html index.htm;
    access_log /var/log/nginx/blog_access.log main;
    }
include /etc/nginx/conf.d/*.conf;
}

2、检查语法并平滑重启

[root@linux-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node2 ~]# systemctl reload nginx

3、写一个测试页面

[root@linux-node2 ~]# echo "bbs.abc.org" > /usr/share/nginx/html/bbs/index.html 
[root@linux-node2 ~]# echo "blog.abc.org" > /usr/share/nginx/html/bbs/index.html 

4、配置域名解析

[root@linux-node2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.11 linux-node1 linux-node1.example.com
192.168.56.12 www.test123.com linux-node2 linux-node2.example.com www.abc.org bbs.abc.org blog.abc.org
[root@linux-node2 ~]# ping bbs.abc.org
PING www.test123.com (192.168.56.12) 56(84) bytes of data.
64 bytes from www.test123.com (192.168.56.12): icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from www.test123.com (192.168.56.12): icmp_seq=2 ttl=64 time=0.048 ms
^C
--- www.test123.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.048/0.076/0.105/0.029 ms
[root@linux-node2 ~]# ping blog.abc.org
PING www.test123.com (192.168.56.12) 56(84) bytes of data.
64 bytes from www.test123.com (192.168.56.12): icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from www.test123.com (192.168.56.12): icmp_seq=2 ttl=64 time=0.051 ms
^C
--- www.test123.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.047/0.049/0.051/0.002 ms

5、测试

[root@linux-node2 ~]# curl bbs.abc.org
bbs.abc.org
[root@linux-node2 ~]# curl blog.abc.org
blog.abc.org

小结:
Nginx 配置虚拟主机流程
(1)复制完整的 server 标签段,粘贴到 http 标签里(一定要放在 http 的结束大括号之内)。
(2)更改 server_name 及对应网页的 root 根目录。
(3)检查配置文件的语法,平滑重启服务
(4) 创建 server_name 对应的网页文件,并建立测试文件。没有 index 首页会报 403 错误
(5) 在客户端 hosts 文件中添加对 server_nameDNS 解析
(6) 在客户端的浏览器访问。

二、Nginx状态监控

1、在配置文件 status.nginx.conf 中增加状态配置

[root@linux-node2 ~]# cat /etc/nginx/conf.d/status.nginx.conf 
server {
    listen 80;
    server_name status.abc.org;
    stub_status on;
    access_log off;
}

2、检查语法并平滑重启

[root@linux-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node2 ~]# systemctl reload nginx

3、配置客户端hosts解析

[root@linux-node2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.11 linux-node1 linux-node1.example.com
192.168.56.12 www.test123.com linux-node2 linux-node2.example.com www.abc.org bbs.abc.org blog.abc.org status.abc.org

4、访问状态页

[root@linux-node2 ~]# ping status.abc.org
PING www.test123.com (192.168.56.12) 56(84) bytes of data.
64 bytes from www.test123.com (192.168.56.12): icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from www.test123.com (192.168.56.12): icmp_seq=2 ttl=64 time=0.051 ms
^C
--- www.test123.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.045/0.048/0.051/0.003 ms
[root@linux-node2 ~]# curl status.abc.org
Active connections: 1 
server accepts handled requests
 181 181 247 
Reading: 0 Writing: 1 Waiting: 0 

5、状态页内容说明

Active connections: 1 
            表示 nginx 正在处理的活动连接数为 1 个
server accepts handled requests
 181 181 247 
      第一个 server 表示 nginx 启动到现在共处理了 181个连接
      第二个 accepts 表示启动到现共成功创建了 181次握手
      第三个 handled requests 表示总共处了 247 次请求
      请求丢失数= accepts(握手数) - server(连接数)

Reading: 0 Writing: 1 Waiting: 0 
      Reading:表示读取到客户端的 Header 信息数
      Writing:表示返回给客户端的 Header 信息数
      Waiting: 已经处理完,正在等待下一次请求指令的驻留连接(开启 keep-alive 的情况下)。
      waiting = Active -(Reading + Writing)

三、Nginx的日志格式

1、修改nginx配置

[root@linux-node2 ~]# cat /etc/nginx/nginx.conf
worker_processes  auto;
error_log /var/log/nginx/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #定义日式访问的格式
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
   server {
    listen 80;
    server_name bbs.abc.org;
    root html/bbs;
    index index.html index.htm;
    access_log /var/log/nginx/bbs_access.log main;      #定义bbs的访问日志
    }
   server {
    listen 80;
    server_name blog.abc.org;
    root html/blog;
    index index.html index.htm;
    access_log /var/log/nginx/blog_access.log main;    #定义blog的访问日志
    }
include /etc/nginx/conf.d/*.conf;
}

2、查看 logs 目录下日志

[root@linux-node2 ~]# ll /var/log/nginx/
总用量 24
-rw-r--r-- 1 nginx nginx    0 16 07:35 access.log
-rw-r--r-- 1 nginx root    93 16 09:03 bbs_access.log
-rw-r--r-- 1 nginx root    93 16 09:03 blog_access.log
-rw-r--r-- 1 nginx nginx    0 16 07:35 error.log
-rw-r--r-- 1 nginx root  3931 15 17:47 test123_access.log
-rw-r--r-- 1 nginx nginx    0 16 07:35 www_access.log
[root@linux-node2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node2 ~]# systemctl reload nginx

3、查看日志并说明

1)查看bbs的访问日志
[root@linux-node2 ~]# tail -1 /var/log/nginx/bbs_access.log 
192.168.56.1 - - [06/Jan/2018:09:16:48 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "http://bbs.abc.org/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-"2)日志格式
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
(3)格式说明
$remote_addr:直接客户端地址
$remote_user:远程客户端用户名称
$time_local:访问时间和时区
$request:用户的请求,使用的 Http 协议
$status: 返回状态,如 200304404 等
$body_bytes_sent:发送的 body 字节数
$http_referer:引用页(从哪个链接访问来的)
$http_user_agent:客户端浏览器类型、版本等信息
$http_x_forwarded_for:间接客户端地址(一般前面会有代理服务器)

4、日志按日期切割

(1)编写切割脚本
[root@linux-node2 ~]# cat /server/scripts/cut_nginx_log.sh 
#!/bin/bash
[ -f /var/log/nginx/www_access.log ] && /bin/mv /var/log/nginx/www_access.log /var/log/nginx/www_access_$(date +%F -d -1day).log && systemctl reload nginx

(2)写入计划任务
[root@linux-node2 ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh &>/dev/null

四、Nginx负载均衡功能实现

1、配置负载均衡lb.conf

#这里是单台服务器的负载均衡实现,多台也是一样的,只是ip和域名不同,原理相通

[root@linux-node2 conf.d]# cat lb.conf 
upstream backend {     #增加负载均衡模块,名称为backend
    server 192.168.56.12:8081 max_fails=3 fail_timeout=30s;
    server 192.168.56.12:8082 max_fails=3 fail_timeout=30s;
}
server {
        listen       80;
        server_name  www.test123.com ;     #定义负载均衡域名
    location / {
        proxy_pass http://backend;          #反向代理
        proxy_set_header Host $host;     #记录主机名
        proxy_set_header X-Forwarded-For $remote_addr;   #记录ip
    }
    access_log /var/log/nginx/test123_access.log main;

 }

2、配置两个不同端口的server

[root@linux-node2 conf.d]# cat nginx_web.conf 
server {
    listen 8081;
    server_name 192.168.56.12;
    root html/www;
    index index.htm index.html index.htm index.php;

}
[root@linux-node2 conf.d]# cat nginx_web2.conf 
server {
    listen 8082;
    server_name 192.168.56.12;
    root html/www2;
    index index.htm index.html index.htm index.php;

}

3、写入测试页面

[root@linux-node2 conf.d]# cat /usr/share/nginx/html/www/index.html 
<h1> Aapche </h1>
[root@linux-node2 conf.d]# cat /usr/share/nginx/html/www2/index.html 
<h1> Nginx </h1>

4、添加hosts解析

[root@linux-node2 conf.d]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.11 linux-node1 linux-node1.example.com
192.168.56.12 www.test123.com linux-node2 linux-node2.example.com www.abc.org bbs.abc.org blog.abc.org status.abc.org

5、访问测试

[root@linux-node2 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node2 conf.d]# systemctl reload nginx
#默认策略是轮询: RR(Round Robin)
[root@linux-node2 conf.d]# for n in `seq 10`;do curl www.test123.com;sleep 2;done
<h1> Aapche </h1>
<h1> Nginx </h1>
<h1> Aapche </h1>
<h1> Nginx </h1>
<h1> Aapche </h1>
<h1> Nginx </h1>
<h1> Aapche </h1>
<h1> Nginx </h1>
<h1> Aapche </h1>
<h1> Nginx </h1>

小结:
Nginx 的负载均衡有健康检查功能,可以探测服务节点是否正常提供服务。

五、集群节点的 Session 会话共享问题

由于 Nginx 的负载均衡默认采用轮询策略: RR(Round Robin),所以当存在用户登陆访问时,有可能在用户登录后,将用户请求分配到其它服务器上,由于该服务器没有 Session,导致认为用户没有登陆,要求用户重新登陆的问题。这时,可以指定 Nginx 的负载均衡策略为 ip_hash,让每个请求按访问 IP 的 hash 结果分配,这样每个访客固定访问一个后端节点服务器,可以有效解决 Session 的问题。 ip_hash 要求 nginx 一定要是最前端的服务器,否则 Nginx 得不到正确的 IP,这时分流是错乱的。 ip_hash 的缺点是会造成负载不均。 用户会话一般保存在服务器的/tmp 目录下。

1、配置 lb.conf上的 Nginx 负载均衡策略为 ip_hash

[root@linux-node2 conf.d]# cat lb.conf 
upstream backend {     #增加负载均衡模块,名称为backend
    ip_hash;    #ip_hash
    server 192.168.56.12:8081 max_fails=3 fail_timeout=30s;
    server 192.168.56.12:8082 max_fails=3 fail_timeout=30s;
}
server {
        listen       80;
        server_name  www.test123.com ;     #定义负载均衡域名
    location / {
        proxy_pass http://backend;          #反向代理
        proxy_set_header Host $host;     #记录主机名
        proxy_set_header X-Forwarded-For $remote_addr;   #记录ip
    }
    access_log /var/log/nginx/test123_access.log main;

 }

2、测试

#从测试过程可以看到同一客户端访问会固定分配到同一台服务器上
[root@linux-node2 conf.d]# for n in `seq 10`;do curl www.test123.com;sleep 2;done
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>
<h1> Nginx </h1>

3、生产场景如何保证 Session 共享

由于轮询 RR 策略无法解决 Session 保持, ip_hash 又可能造成后台服务器负载不均,所在在生产环境中一般是
在所有节点服务器之外搭建缓存服务器,来保存 Session 会话信息。缓存服务器使用的软件一般是 Memcached、Redis、
Nosql 等。 集群架构多服务器同步 Session 的主要方式:
(1)负载均衡层(LB 层):都会导致负载不均,小流量用,大流量不用
LVS: lvs -p
Nginx: ip_hash
Haproxy: cookie insert2)软件层:主要是 session 复制。如 Tomcat、 resin couchbase
(3)共享: Memcache 或其它 nosql 工具, PHP 常用这类方式
(4)门户网站:主要用 cookies 或 cookies 配合 session 把用户级会话缓存在用户本


本文转自 IT_外卖小哥  51CTO博客,原文链接:http://blog.51cto.com/jinlong/2058041

相关文章
|
17天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
22 0
|
28天前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
38 0
|
2月前
|
应用服务中间件 PHP 开发工具
Nginx解析环境搭建及实战
Nginx解析环境搭建及实战
25 0
|
2月前
|
负载均衡 Ubuntu 应用服务中间件
|
16天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
30 2
|
2月前
|
前端开发 应用服务中间件 Linux
nginx解决springcloud前后端跨域问题,同时配置ssl
nginx解决springcloud前后端跨域问题,同时配置ssl
|
16天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
78 0
|
1月前
|
PHP
百度虚拟机 bcloud_nginx_user.conf配置
百度虚拟机 bcloud_nginx_user.conf配置
22 0
|
10天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
24 0
|
20天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
27 3