nginx

一.Nginx的介绍
1.概念
nginx(发音”engine x”)是俄罗斯软件工程师Igor Sysoev开发的免费开源web服务器软件。nginx于2004年发布,聚焦于高性能,高并发和低内存消耗问题。并且具有多种web服务器功能特性:负载均衡,缓存,访问控制,带宽控制,以及高效整合各种应用的能力,这些特性使nginx很适合于现代网站架构。目前,nginx已经是互联网上第二流行的开源web服务器软件。
2.架构图 在这里插入图片描述

二.Nginx的安装(源码方式)
1.下载源码包,并解压

注意,在下载安装包的过程中,一定要看和当前的linux操作系统是否匹配,省的做一些无用功
[root@hx ~]# tar zxf nginx-1.12.0.tar.gz
[root@hx ~]# cd nginx-1.12.0
[root@hx nginx-1.12.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@hx nginx-1.12.0]# cd src/
[root@hx src]# ls
core event http mail misc os stream
[root@hx src]# cd core/
[root@hx core]# ls
nginx.c ngx_inet.h ngx_radix_tree.h
nginx.h ngx_list.c ngx_rbtree.c

2.隐藏nginx的版本号,在这里主要是为了安全起见
vim /root/nginx-1.12.0/src/core/nginx.h

3.关闭gcc的debug调试模式,节省空间

[root@hx nginx-1.12.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@hx nginx-1.12.0]# cd auto/
[root@hx auto]# ls
cc feature headers install module options stubs types
define have include lib modules os summary unix
endianness have_headers init make nohave sources threads
[root@hx auto]# cd cc
[root@hx cc]# ls
acc bcc ccc clang conf gcc icc msvc name owc sunc
[root@hx cc]# vim gcc
在这里插入图片描述

4.设置nginx的目录及各种模块比如openssl加密等

因为linux内核是c语言写的,所以一定要保证gcc环境
#yum install gcc -y

[root@hx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-file-aio --with-threads --with-http_stub_status_module

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

编译过程中出现任何问题,看error,缺少pcre模块
[root@hx nginx-1.12.0]# yum install -y pcre-devel

继续编译
./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.
缺少OpenSSL
[root@hx nginx-1.12.0]# yum install -y openssl-devel

成功标志:
Configuration summary

  • using threads
  • using system PCRE library
  • using system OpenSSL library
  • 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”

5.make编译

[root@hx nginx-1.12.0]# make

6.安装编译后的包

[root@hx nginx-1.12.0]# make install

7.创建nginx的本地家目录,uid:800

[root@hx conf]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin -u 800 nginx

8.进入到/var/local/nginx/sbin下,为方便调用做软连接
-t 检测nginx语法是否错误

[root@server1 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

9.用ps ax 查看进程,nginx正常开启
[root@server1 sbin]# nginx
[root@server1 sbin]# nginx -s reload
[root@server1 sbin]# ps ax
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 /sbin/init
2 ? S 0:00 [kthreadd]
3 ? S 0:00 [migration/0]

9237 ? Ss 0:00 nginx: master process nginx
9240 ? S 0:00 nginx: worker process
9241 pts/0 R+ 0:00 ps ax

三.Nginx多核绑定

[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@server1 conf]# vim nginx.conf

worker_processes 2; 二核CPU,开启2个进程
worker_cpu_affinity 01 10; 01和10分别表示第一个CPU和第二个CPU(三个001,010,100)
worker_connections 65535; 连接数,65535为最大的进程数

修改内核最大连接参数

[root@server1 conf]# vim /etc/security/limits.conf
在这里插入图片描述
四.配置nginx证书加密
1.配置文件

[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf

2.生成证书,pem为私钥,在此目录下查看Makefile可知包含证书与加密

[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# vim 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.KHbLK3’

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) []:shannxi
Locality Name (eg, city) [Default City]:xi’an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server’s hostname) []:server1
Email Address []:huangxin@westos.org

3.移至/usr/local/nginx/conf/下,重新加载nginx,使之生效

[root@server1 certs]# ls
ca-bundle.crt cert.pem Makefile
ca-bundle.trust.crt make-dummy-cert renew-dummy-cert
[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 sbin]# nginx
[root@server1 sbin]# nginx -s reload

nginx添加memcache模块*****
1.什么是OpenResty?

OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web平台,由中国人章亦春发起,提供了很多高质量的第三方模块。
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。

OpenResty 的目标是让你的 Web 服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL,PostgreSQL,~Memcaches 以及 ~Redis 等都进行一致的高性能响应。
所以对于一些高性能的服务来说,可以直接使用 OpenResty 访问 Mysql或Redis等,而不需要通过第三方语言(PHP、Python、Ruby)等来访问数据库再返回,这大大提高了应用的性能。
参考openresty中文官网 http://openresty.org/cn/

原nginx不支持memcache
1)给nginx添加memc和sr cache模块,让nginx直接访问memcache来提高速度
先关闭原来的nginx,因为下面安装的openresty-1.13.6.1.tar.gz也是nginx

nginx -s stop

源码编译安装openresty-1.13.6.1.tar.gz(不需要加参数,使用默认)
./configure --prefix=/usr/local/openresty
gmake && gmake install

拷贝之前example.php和index.php到默认发布目录准备测试
/usr/local/openresty/nginx/html
[root@server5 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server5 html]# cp /usr/local/lnmp/nginx/html/example.php .

修改nginx配置文件:vim /usr/local/openresty/nginx/conf/nginx.conf
添加:
upstream memcache {
server localhost:11211;
keepalive 512;
}

    location /memc {
        internal;  # 只接收内部访问,不接受外部http访问。比较安全
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;   ##后端服务器数据回传时间
        memc_read_timeout 100ms;   ##连接成功后,后端服务器响应时间
        set $memc_key $query_string;
        set $memc_exptime 300;
        memc_pass memcache;
    }

    location ~ \.php$ {
        set $key $uri$args;
    ##http的GET方法表示get、PUT方法表示set
        srcache_fetch GET /memc $key;(这两个配置的作用是:请求php页面时,先会去memcache中找,如果没有,正常访问;
        srcache_store PUT /memc $key; 访问结束后将结果存到memcache,下次访问时直接从缓存中)
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi.conf;
    }

再次进行压测,速度会比之前只加了php的缓存更快

###重点说明一下对index.php页面的访问
访问速度有了明显的差别 因为php是没有缓存的
tomcat

1)安装jdk和tomcat
tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/

2)做好软连接便于访问
cd /usr/local
ln -s jdk1.7.0_79/ java
ln -s apache-tomcat-7.0.37/ tomcat

3)配置环境变量
vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.: J A V A H O M E / l i b : JAVA_HOME/lib: JAVAHOME/lib:JAVA_HOME/jre/lib
export PATH= P A T H : PATH: PATH:JAVA_HOME/bin
source /etc/profile
#####################################################################################
[root@server1 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin:/usr/local/java/bin
[root@server1 local]# echo $CLASSPATH
.:/usr/local/java/lib:/usr/local/java/jre/lib
[root@server1 local]# echo $JAVA_HOME
/usr/local/java
[root@server1 local]# which java
/usr/local/java/bin/java
[root@server1 local]# which javac
/usr/local/java/bin/javac

vim test.java
public class test
{
public static void main(String[] args)
{
System.out.println(“Hello World1”);
}
}
javac test.java
java test
cd /usr/local/tomcat
bin/startup.sh
网页访问:172.25.0.1:8080
网页访问:172.25.0.2:8080
编写页面测试:
cd /usr/local/tomcat/webapps/ROOT/
vim test.jsp
server1-The time is: <%=new java.util.Date() %>

测试nginx对tomcat的负载均衡
vim nginx.conf
upstream tomcat {
server 172.25.0.1:8080;
server 172.25.0.2:8080;

}
location / {
root /usr/local/tomcat/webapps/ROOT;
index index.html index.htm;
}
location ~ .jsp$ {
proxy_pass http://tomcat;
}

http://172.25.0.101/test.jsp
#####################################################################################
4)增加测试页面:vim /usr/local/apache-tomcat-7.0.37/webapps/ROOT/test.jsp
<%@ page contentType=“text/html; charset=GBK” %>
<%@ page import=“java.util.*” %>

Cluster App Test Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%> <% out.println("
ID " + session.getId()+"
"); String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.print(" Session list"); Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println( name + " = " + value+"
"); System.out.println( name + " = " + value); } %> name:
key:

5)在nginx配置文件中增加tomcat模块,为了实现session共享,需要支持sticky(粘滞)模块,nginx-1.14不支持sticky,所以使用nginx-1.10版本,重新编译nginx
###注意:将/usr/local/lnmp/nginx 目录全部删除 再次编译
tar zxf nginx-sticky-module-ng.tar.gz -C /usr/local/
cd nginx-1.10.3
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/usr/local/nginx-sticky-module-ng
make && make install
ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/
vim /usr/local/lnmp/nginx/conf/nginx.conf
upstream tomcat {
sticky;
server 172.25.0.1:8080;
server 172.25.0.2:8080;

}
gzip on; #开启gzip
location / {
root /usr/local/tomcat/webapps/ROOT;
index index.html index.htm;
}
location ~ .jsp$ {
proxy_pass http://tomcat;
}
nginx
再次刷新页面,访问的ID号不会变,实现了sticky

接下来实现session共享:

1)进入/usr/local/tomcat/lib目录(server1,2都做)
将以下jar包拷贝到该目录:
asm-3.2.jar
kryo-1.04.jar
kryo-serializers-0.10.jar
memcached-session-manager-1.6.3.jar
memcached-session-manager-tc7-1.6.3.jar(最重要)
minlog-1.2.jar
msm-kryo-serializer-1.6.3.jar
reflectasm-1.01.jar
spymemcached-2.7.3.jar

2)编辑/usr/local/tomcat/conf/context.xml文件,加入session共享配置
context.xml 该文件的作用是配置各种数据库的连接池
#########################################################
XML概念
xml常用于数据存储和传输,文件后缀为 .xml
它是可扩展标记语言(Extensible Markup Language,简称XML),是一种标记语言
标记,指计算机所能理解的信息符号;
通过此种标记,计算机之间可以处理包含各种信息的文章等
1 标记,指计算机所能理解的信息符号;
2 通过此种标记,计算机之间可以处理包含各种信息的文章等。
XML设计用来传送及携带数据信息,不用来表现或展示数据,所以XML用途的焦点是它说明数据是什么,以及携带数据信息。而HTML语言则用来表现数据

#########################################################
<Manager className=“de.javakaffee.web.msm.MemcachedBackupSessionManager”
memcachedNodes=“n1:172.25.230.5:11211,n2:172.25.230.6:11211”
failoverNodes=“n1” (在172.25.230.6上"n1"改为"n2")
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass=“de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory”
/>

server1:
yum install memcached -y
/etc/init.d/memcached start

server2:
3)保存并启动tomcat
172.25.0.1/test.jsp

4)再次测试,如果n1挂了,session不会被重置

###注意测试的时候:想更改n1 n2 的时候 可以手动停掉server1 server2上的tomcat和memcached
##停止一端的memcache
####################################################################
一、cookie:

在网站中,http请求是无状态的。也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户。cookie的出现就是为了解决这个问题,第一次登录后服务器返回一些数据(cookie)给浏览器,然后浏览器保存在本地,当该用户发送第二次请求的时候,就会自动的把上次请求存储的cookie数据自动的携带给服务器,服务器通过浏览器携带的数据就能判断当前用户是哪个了。cookie存储的数据量有限,不同的浏览器有不同的存储大小,但一般不超过4KB。因此使用cookie只能存储一些小量的数据。
二、session:

session和cookie的作用有点类似,都是为了存储用户相关的信息。不同的是,cookie是存储在本地浏览器,而session存储在服务器。存储在服务器的数据会更加的安全,不容易被窃取。但存储在服务器也有一定的弊端,就是会占用服务器的资源,但现在服务器已经发展至今,一些session信息还是绰绰有余的。
三、cookie和session结合使用:

web开发发展至今,cookie和session的使用已经出现了一些非常成熟的方案。在如今的市场或者企业里,一般有两种存储方式:

1、存储在服务端:通过cookie存储一个session_id,然后具体的数据则是保存在session中。如果用户已经登录,则服务器会在cookie中保存一个session_id,下次再次请求的时候,会把该session_id携带上来,服务器根据session_id在session库中获取用户的session数据。就能知道该用户到底是谁,以及之前保存的一些状态信息。这种专业术语叫做server side session。

2、将session数据加密,然后存储在cookie中。这种专业术语叫做client side session。flask采用的就是这种方式,但是也可以替换成其他形式
####################################################################

nginx的 keepalive_timeout参数是一个请求完成之后还要保持连

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值