Nginx实战

apache不支持高并发,nginx能支持处理百万级的TCP连接,10万以上的并发连接。

nginx能实现负载均衡,首先解释一下负载均衡:

当N多用户访问服务器的时候,为了减少服务器压力,需要将用户分别引入各服务器,分担服务器压力。

(一) nginx环境的搭建

安装流程:

1 通过ftp将nginx-1.11.8.tar.gz文件上传到服务器;

2 解压: tar -zxvf nginx-1.11.8.tar.gz;

3 安装:

cd nginx-1.11.8

./configure

make install

nginx搭建过程中常见问题

在Linux操作系统下搭建Nginx服务器,很多时候会出现不同的错误,在此,我们对搭建过程中出现的错误进行一些总结。

主要有这些类型:防火墙问题,缺少gc++,缺少pcre、zlib等库。

安装gcc: yum -y install gcc gcc-c++ autoconf automake

安装PCRE library: yum -y install pcre pcre-devel

安装zlib library: yum -y install zlib zlib-devel

安装完后的提示信息:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

(二) Nginx的启动

启动代码格式:Nginx -c Nginx配置文件地址

启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

(三) Nginx停止

从容停止:kill -QUIT 主进程号

快速停止:kill -TERM 主进程号

kill -INT 主进程号

强制停止:pkill -9 nginx

killall -9 nginx //强制杀死进程nginx

(四) Nginx重启

进入sbin目录,执行:./nginx -s reload

或者:kill HUP 主进程号

(五) 验证配置是否正常

进入sbin目录,执行:./nginx -t

或者:usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

(六) Nignx的信号控制

nginx常见的信号控制:

HUP 重启

QUIT 从容关闭

TERM 快速关闭

INT 从容关闭

USR1 切换日志文件

USR2 平滑升级可执行进程

WINCH 从容关闭工作进程

(七) nginx的平滑升级

进入sbin目录,执行:

./nginx -V 【查看版本】

平滑升级:

tar -zxvf nginx-1.9.2.tar.gz

./configure

make

cp nginx nginx.old 【备份】

cp -rfp objs/nginx /usr/local/nginx/sbin 【强制覆盖】

nginx.conf配置文件详解:

1 #设置用户群,比如:user root ,只能被root使用
  2 #user  nobody;
  3 
  4 #工作进程数:最好为cpu的单数倍或者双倍
  5 worker_processes  1;
  6 
  7 #设置错误存放文件存放路径
  8 #error_log  logs/error.log;
  9 
 10 #notice,info类型的错误日志存放路径
 11 #error_log  logs/error.log  notice;
 12 #error_log  logs/error.log  info;
 13 
 14 #设置pid存放路径(pid是控制系统中重要文件)
 15 #pid        logs/nginx.pid;
 16 
 17 #设置最大连接数
 18 events {
 19     worker_connections  1024;
 20 }
 21 
 22 #设置http相关信息
 23 http {
 24     include       mime.types;
 25     default_type  application/octet-stream;
 26 
 27     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 28     #                  '$status $body_bytes_sent "$http_referer" '
 29     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 30  
 31     #access_log  logs/access.log  combined;
 32 
 33     sendfile        on;
 34     #tcp_nopush     on;
 35 
 36     #keepalive_timeout  0;
 37     keepalive_timeout  65;
 38 
 39     #支不支持gzip压缩,如果开启,用户在访问网页时,传输给用户的文件是压缩后的文件
 40     #是原来文件的30%
 41     #gzip  on;
 42 
 43     server {
 44         listen       8081;
 45         server_name  localhost;
 46 
 47         #设置字符编码,如:charset utf-8
 48         #charset koi8-r;
 49 
 50         #access_log  logs/host.access.log  main;
 51 
 52         location / {
 53             root   html;
 54             index  index.html index.htm;
 55         }
 56 
 57         #error_page  404              /404.html;
 58 
 59         # redirect server error pages to the static page /50x.html
 60         #
 61         error_page   500 502 503 504  /50x.html;
 62         location = /50x.html {
 63             root   html;
 64         }
 65 
 66         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 67         #
 68         #location ~ \.php$ {
 69         #    proxy_pass   http://127.0.0.1;
 70         #}
 71 
 72         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 73         #
 74         #location ~ \.php$ {
 75         #    root           html;
 76         #    fastcgi_pass   127.0.0.1:9000;
 77         #    fastcgi_index  index.php;
 78         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 79         #    include        fastcgi_params;
 80         #}
 81 
 82         # deny access to .htaccess files, if Apache's document root
 83         # concurs with nginx's one
 84         #
 85         #location ~ /\.ht {
 86         #    deny  all;
 87         #}
 88     }
 89 
 90 
 91     # another virtual host using mix of IP-, name-, and port-based configuration
 92     #
 93     #server {
 94     #    listen       8000;
 95     #    listen       somename:8080;
 96     #    server_name  somename  alias  another.alias;
 97 
 98     #    location / {
 99     #        root   html;
100     #        index  index.html index.htm;
101     #    }
102     #}
103 
104 
105     # HTTPS server
106     #
107     #server {
108     #    listen       443 ssl;
109     #    server_name  localhost;
110 
111     #    ssl_certificate      cert.pem;
112     #    ssl_certificate_key  cert.key;
113 
114     #    ssl_session_cache    shared:SSL:1m;
115     #    ssl_session_timeout  5m;
116 
117     #    ssl_ciphers  HIGH:!aNULL:!MD5;
118     #    ssl_prefer_server_ciphers  on;
119 
120     #    location / {
121     #        root   html;
122     #        index  index.html index.htm;
123     #    }
124     #}
125 
126 }

(八)日志管理

1 log_format  combinded  
2                          '$remote_addr - $remote_user [$time_local] "$request" '
3                          '$status $body_bytes_sent "$http_referer" '
4                          '"$http_user_agent" "$http_x_forwarded_for"';
5   
6 access_log  logs/access.log  combined;

nginx日志文件的切割配置:

touch cutlog.sh

在chutlog.sh中输入:

D=$(data +%y%m%d)

mv /usr/local/nginx/logs/access.log ${D}.log

kill -USR1 $(cat /usr/local/nginx/nginx.pid)

保存。

编辑定时文件:

crontab -e

//每天的23时59分定时执行cutlog.sh批处理文件

输入:23 59 *** /bin/bash /usr/local/nginx/logs/cutlog.sh

保存。

(九)缓存配置

location ~.*.(jpg|png|gif)$ {//以jpg结尾的文件

expires 30d;//缓存30天

}

location ~.*.(css|js)?$ {

expiress 1h;

}

(十)gzip配置

通过gzip压缩技术,可以是原来的网页大小压缩成原来的30%。

gzip配置:

gzip on;

gzip_min_length 1k;//小于1k的文件不压缩,因为小于1k的文件执行压缩可能起不到压缩的作用,甚至有可能会比压缩前的大

gzip_buffers 4 16k;//4个16k的数据流

gzip http version 1.1;//可识别http版本

gzip_vary on; //开启判断浏览器是否支持gzip压缩技术,如果客户端浏览器不支持压缩技术,服务器就不进行gzip压缩

(十一)自动列目录功能

location / {

index index.html index.htm;

root html;

autoindex on;//开启自动列目录

}

(十二)tomcat与niginx建立联系

location ~.(jsp|do)?$ {

root /data0/www;

index index.jsp index.do;

proxy_set_header X_Forwarded_Host $host;

proxy_set_header X_Forwarder_For $proxy_add_x_forwarded_for;

proxy_pass http://localhost:8080; //tomcat服务

}

(十三) 负载均衡的实现原理

正向代理:明确知道要访问的服务器地址。

Nginx实战

反向代理:不知道真实访问服务器是集群中的那一台。

Nginx实战

负载均衡:多个服务器组成一个服务器集群,用户访问一个中间服务器(代理服务器),再让中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该选择的服务器。

nginx是一款可以通过反向代理实现负载均衡的服务器。

upstream模块实现在轮询和客户端IP之间实现后端的负载均衡。

upstream myserver {

//ip_hash; //同一个用户第二次访问时分配第一次分配的服务器(不建议使用,ip_hash有一些缺陷)

server 182.18.79.234 weight=2; //访问分配的权重,默认为1

server 140.205.23.234;

}

server {

listen 8080;

location \ {

proxy_pass http://myserver;

}

}

nginx中的ip_hash技术能够将某个ip的请求定向到同一台后端,这样一来这个ip下的某个客户端和某个后端就能建立起稳固的session,ip_hash是在upstream配置中定义的。

ip_hash是容易理解的,但是因为仅仅能用ip这个因子来分配后端,因此ip_hash是有缺陷的,不能在一些情况下使用:

nginx不是最前端的服务器

ip_hash要求nginx一定是最前端的服务器,否则nginx得不到正确ip,就不能根据ip作hash。譬如使用的是squid为最前端,那么nginx取ip时只能得到squid的服务器ip地址,用这个地址来作分流是肯定错乱的。

nginx的后端还有其它方式的负载均衡

假如nginx后端又有其它负载均衡,将请求又通过另外的方式分流了,那么某个客户端的请求肯定不能定位到同一台session应用服务器上。

工作当中,整理出来了一些运维手册:Nginx运维手册

原文链接: https://www.cnblogs.com/yucongblog/p/6289628.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/247904

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午2:32
下一篇 2023年2月14日 上午2:33

相关推荐