lnmp架构(第一篇)

lnmp 架构 第一篇

nginx 源码安装

nginx的安装包:nginx-1.12.0.tar.gz


建议安装前的修改:

  1. 在nginx的解压包中修改文件nginx-1.12.0/src/core/nginx.h:去掉nginx后面的NGINX_VERSION。此举是为了不显示nginx的版本,提高安全性。

#define NGINX_VER "nginx/"NGINX_VERSION

  1. 在nginx的解压包中修改文件nginx-1.12.0/auto/cc/gcc:注释掉debug的编译方式

# debug
#CLAGS="$CFLAGS -g"


nginx的安装:

  1. # ./configure

没有error即为编译成功
如果有error则可根据error解决依赖性
例如:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or * install the OpenSSL library *into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.

根据error我安装了openssl-devel
  1. # make
  2. # make install

安装结束后,由./configure时--prefix指定的路径确定nginx的安装位置

/usr/local/lnmp/nginx/sbin/nginx

为了使用方便建议创建超连接:(超连接时使用绝对路径)

[root@server1 sbin]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/

nginx的启动:

  1. 检查nginx:(没有error即可启动nginx)

# nginx -t

  1. 启动nginx
  2. 查看nginx的监听端口:80端口
[root@server1 ~]# netstat -antlp |grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*  
LISTEN      6187/nginx          
  1. 浏览器访问安装nginx的主机IP(启动浏览器的主机要与启动nginx的主机能相互ping通),显示nginx的默认页

nginx的默认发布目录:/usr/local/lnmp/nginx/html
可在nginx的默认发布目录中创建新的网页文件,浏览器访问* IP/文件名 *及可

nginx的配置文件:/usr/local/lnmp/nginx/conf/nginx.conf

定义nginx运行的用户及用户组
user	nginx nginx;

nginx检查报错--->查看发现没有nginx用户--->建立nginx用户在刷新nginx配置文件
nginx指定用户及用户组的前提是指定用户及用户组必须已经存在

[root@server1 conf]# nginx -t
nginx: [emerg] getpwnam("nginx") failed in /usr/local/lnmp/nginx/conf/nginx.conf:2
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test failed
[root@server1 conf]# id nginx
id: nginx: No such user
[root@server1 conf]# useradd nginx
[root@server1 conf]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 conf]# nginx -t 
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload
nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;

nginx配置文件默认为worker——process 1;查看进程发现启动一个master进程和一个worker进程

 1080 ?        Ss     0:00 nginx: master process nginx
 1081 ?        S      0:00 nginx: worker process
 1090 pts/0    R+     0:00 ps ax

修改后再次查看进程,发现启动一个master进程和多个worker进程

 1080 ?        Ss     0:00 nginx: master process nginx
 1111 ?        S      0:00 nginx: worker process
 1112 ?        S      0:00 nginx: worker process
 1113 ?        S      0:00 nginx: worker process
 1114 ?        S      0:00 nginx: worker process
 1115 ?        S      0:00 nginx: worker process
 1116 ?        S      0:00 nginx: worker process
 1117 ?        S      0:00 nginx: worker process
 1118 ?        S      0:00 nginx: worker process
 1119 pts/0    R+     0:00 ps ax

nginx是以多进程的方式来工作的,nginx启动后会有一个master进程和多个worker进程。master进程用来监控worker进程包括:接受外界信号,并向worker进程发送信号;监控worker的运行状态,当某个worker进程异常退出时,master进程会重新fork出一个新的worker进程。而基本的网络事件则交由worker进程处理,一个请求只能有一个worker进程处理。
nginx的处理过程大概时这样的:master进程会先建立好需要listen的socket,并fork子进程workers来即成相应的socket(注意:每个worker进程的socket并不是同一个socket,只是他们都会监听在同一个IP地址上的同一个端口)

工作模式与连接数上限,即nginx可以打开的最多文件描述符数目。
events {
    worker_connections  1024;
}

worker_connecions和worker_rlimit_nofile

查看日志,有一个[warn]: 3660#0: 20000 worker_connections are more than open file resource limit: 1024 !!
原来安装好nginx之后,默认最大的并发数为1024,如果你的网站访问量过大,已经远远超过1024这个并发数,那你就要修改worker_connecions这个值 ,这个值越大,并发数也有就大。当然,你一定要按照你自己的实际情况而定,也不能设置太大,不能让你的CPU跑满100%。
所以,当你修改提高了配置文件中的worker_connections值,然后重启nginx,你就会在日志里发现,最前面我们讲到的这一个warn警告提示,大概的意思就是: 20000并发连接已经超过了打开文件的资源限制:1024!在这种情况下,我们就要修改配置文件,添加一行来解除这个限制,这就好像是apache中的ServerLimit。
打开配置文件在"event"这行上面添加这一行:
worker_rlimit_nofile xxxxx; ####Specifies the value for maximum file descriptors that can be opened by this process.
注意:设置了这个后,你修改worker_connections值时,是不能超过worker_rlimit_nofile的这个值,不然又会有前面的那个warn提示。
保存配置文件,退出重启nginx。
如果nginx 中worker_connections 值设置是1024,worker_processes 值设置是4,按反向代理模式下最大连接数的理论计算公式:
最大连接数 = worker_processes * worker_connections/4
查看相关资料,生产环境中worker_connections 建议值最好超过9000,计划将一台nginx 设置为10240,再观察一段时间。

注明:这是我在网上看见的一个错误解析,结合这个会很好理解这两个参数
keepalive超时时间
keepalive_timeout  65;
虚拟主机配置
server {
        listen 80;
        server_name www.test.org;

        location / {
                root /var/www/html;
                index index.html;
        }
}

这段很简单,但是我要解释的是server_name。server_name指定访问域名,当我修改完配置文件,并在/var/www/html中创建index.html文件之后,浏览器访问www.test.org失败,访问IP则是nginx的默认页。然后我在浏览器坐在主机上进行相应的域名解析之后,在此访问域名,浏览器成功显示了/var/www/html/index.html的内容。与访问IP相同,访问 * 域名/文件 * 则显示/var/www/html目录下相应的网页文件。

server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
}
[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# ll Makefile
-rw-r--r--. 1 root root 2242 Sep 27  2013 Makefile
[root@server1 certs]# make cert.pem
umask 77 ; \
	PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
	PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
	/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \
	cat $PEM1 >  cert.pem ; \
	echo ""    >> cert.pem ; \
	cat $PEM2 >> cert.pem ; \
	rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
...................................................................................+++
...............+++
writing new private key to '/tmp/openssl.0nER84'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:school
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:student
Email Address []:960482927@qq.com
[root@server1 certs]# ls
ca-bundle.crt  ca-bundle.trust.crt  cert.pem  make-dummy-cert  Makefile  renew-dummy-cert
[root@server1 certs]# mv cert.pem /usr/local/lnmp/nginx/conf/
[root@server1 certs]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# ls
cert.pem              fastcgi_params          koi-win             nginx.conf          scgi_params.default   win-utf
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default  uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params         uwsgi_params.default
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

打开浏览器访问https://172.25.44.1即可。

nginx的重定向
server {
        listen 80;
        server_name www.black.org;

        rewrite ^(.*) http://www.write.org$1 permanent;
}

server {
        listen 80;
        server_name www.write.org;

        location / {
                root /web2;
                index index.html;
        }
}
将所有对www.black.org的访问重定向到www.write.org。例如将www.black.org/test.html重定向为www.write.org/test.html。
nginx的负载均衡
upstream linux {
	server 172.25.44.2:80;
    server 172.25.44.3:80;

    server 172.25.44.1:8080 backup;
}
server {
        listen 80;
        server_name www.write.org;

        location / {
                proxy_pass http://linux;
        }
}

172.25.44.2和172.25.44.3上分别开启httpd服务,浏览器访问www.write.org

[root@foundation44 images]# for i in {1..10};do curl www.write.org;done

server3

server3

server3

server2

server2

server3

server2

server2

server3

server2

[root@foundation44 images]# for i in {1..10};do curl www.write.org;done

server2

server3

server3

server3

server2

server2

server3

server2

server2

server3

> ``` --- > 当一台http服务器挂掉后所有的请求就交由其他的http服务器处理。当所有的http服务器都挂掉后我们还可以使用备用web网页即172.25.44.1:8080来向用户说明情况,避免用户的不舒适体验。

posted @ 2017-09-03 21:35  季凉末一  阅读(163)  评论(0编辑  收藏  举报