前言
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。(百度百科)
1.Nginx安装
我使用的环境是64位 Ubuntu 14.04。nginx依赖以下模块:
- gzip模块需要 zlib 库
- rewrite模块需要 pcre 库
- ssl 功能需要openssl库
1.1.安装pcre
1.2.安装openssl
1.3.安装zlib
1.4.安装nginx
下载pcre、openssl、zlib、及其nginx安装包 下载地址:http://download.csdn.net/download/liu976180578/10134160- 获取pcre编译安装包 下载地址:http://download.csdn.net/download/liu976180578/10134160
- 解压缩pcre-xx.tar.gz包。
- 进入解压缩目录,执行./configure。
- make & make install
- 获取openssl编译安装包 下载地址:http://download.csdn.net/download/liu976180578/10134160
- 解压缩openssl-xx.tar.gz包。
- 进入解压缩目录,执行./config。
- make & make install
- 获取zlib编译安装包 下载地址:http://download.csdn.net/download/liu976180578/10134160
- 解压缩openssl-xx.tar.gz包。
- 进入解压缩目录,执行./configure。
- make & make install
- 获取nginx
- 解压缩nginx-xx.tar.gz包。 下载地址:http://download.csdn.net/download/liu976180578/10134160
- 进入解压缩目录,执行./configure
- make & make install
安装nginx注意事项
若安装时找不到上述依赖模块,使用--with-openssl=<openssl_dir>、--with-pcre=<pcre_dir>、--with-zlib=<zlib_dir>指定依赖的模块目录。
nginx解压包下命令
./configure --with-openssl=<openssl_dir> --with-pcre=<pcre_dir> --with-zlib=<zlib_dir>
启动nginx
安装成功后,系统默然会把nginx放在/usr/local/nginx 目录下
进入/usr/local/nginx 目录, 执行启动命令 sbin/nginx
重启nginx
必须cd 到sbin目录,必须是在已启动时执行
cd /usr/local/nginx/sbin sudo
./nginx -s reload
启动nginx之后,浏览器中输入http://localhost可以验证是否安装启动成功。
Nginx的简单配置
- #Nginx创建进程所用用户和用户组
- #user nobody;
- user test test;
- #进程数,一般等于CPU数量
- worker_processes 4;
- #错误日志,定义到warn级别比较合适
- #error_log logs/error.log;
- error_log logs/error.log warn;
- #指定pid存放文件
- pid logs/nginx.pid;
- events {
- #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
- use epoll;
- #允许最大连接数
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #关闭日志能够减少IO,但是建议开启error_log warn级别
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #HTTP连接的持续时间
- keepalive_timeout 30;
- #压缩设置
- gzip on;
- gzip_types text/javascript text/plain text/css application/xml application/x-javascript;
- #Tomcat集群配置
- upstream myserver {
- #通过client的IP进行映射
- ip_hash;
- # 负载均衡设置,weight越大越优先分配
- server localhost:18080 weight=5;
- server localhost:18081 weight=4;
- }
- server {
- listen 8080; #监听的端口号
- server_name localhost;
- charset utf-8; #字符编码方式
- #关闭日志能够减少IO
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- #代理配置
- proxy_pass http://myserver;
- #关闭重定向
- #proxy_redirect off;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
一个机器上运行多个tomcat :
http://blog.csdn.net/T_ZZZ/article/details/76906599
redis缓存共享session :
1、下载三个jar ,下载地址:http://download.csdn.net/download/liu976180578/10134194
下好后,放入tomcat的lib里面,每个tomcat 都要放。
2、打开每个tomcat 的 conf目录下的context.xml文件,在最后一个节点</Context>上面添加。
<Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="192.168.145.130" port="7000" database="0" maxInactiveInterval="60" />
Nginx详情配置
安装完成之后,配置目录conf下有以下配置文件,过滤掉了xx.default配置:
linuxidc@ubuntu:/opt/nginx-1.7.7/conf$ tree |grep -v default . ├── fastcgi.conf ├── fastcgi_params ├── koi-utf ├── koi-win ├── mime.types ├── nginx.conf ├── scgi_params ├── uwsgi_params └── win-utf |
除了nginx.conf,其余配置文件,一般只需要使用默认提供即可。
2.1.nginx.conf
nginx.conf是主配置文件,默认配置去掉注释之后的内容如下图所示:
l worker_process表示工作进程的数量,一般设置为cpu的核数
l worker_connections表示每个工作进程的最大连接数
l server{}块定义了虚拟主机
n listener监听端口
n server_name监听域名
n location{}是用来为匹配的 URI 进行配置,URI 即语法中的“/uri/”。location / { }匹配任何查询,因为所有请求都以 / 开头。
u root指定对应uri的资源查找路径,这里html为相对路径,完整路径为/opt/ opt/nginx-1.7.7/html/
u index指定首页index文件的名称,可以配置多个,以空格分开。如有多个,按配置顺序查找。
从配置可以看出,nginx监听了80端口、域名为localhost、跟路径为html文件夹(我的安装路径为/opt/nginx-1.7.7,所以/opt/nginx-1.7.7/html)、默认index文件为index.html, index.htm、服务器错误重定向到50x.html页面。
可以看到/opt/nginx-1.7.7/html/有以下文件:
linuxidc@ubuntu:/opt/nginx-1.7.7/html$ ls 50x.html index.html |
这也是上面在浏览器中输入http://localhost,能够显示欢迎页面的原因。实际上访问的是/opt/nginx-1.7.7/html/index.html文件。
2.2.mime.types
文件扩展名与文件类型映射表,nginx根据映射关系,设置http请求响应头的Content-Type值。当在映射表找不到时,使用nginx.conf中default-type指定的默认值。例如,默认配置中的指定的default-type为application/octet-stream。
include mime.types;
default_type application/octet-stream;
默认
下面截一段mime.types定义的文件扩展名与文件类型映射关系,完整的请自行查看:
2.3.fastcgi_params
nginx配置Fastcgi解析时会调用fastcgi_params配置文件来传递服务器变量,这样CGI中可以获取到这些变量的值。默认传递以下变量:
这些变量的作用从其命名可以看出。
2.4.fastcgi.conf
对比下fastcgi.conf与fastcgi_params文件,可以看出只有以下差异:
linuxidc@ubuntu:/opt/nginx-1.7.7/conf$ diff fastcgi.conf fastcgi_params 2d1 < fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
即fastcgi.conf只比fastcgi_params多了一行“fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;”
原本只有fastcgi_params文件,fastcgi.conf是nginx 0.8.30 (released: 15th of December 2009)才引入的。主要为是解决以下问题(参考:
原本Nginx只有fastcgi_params,后来发现很多人在定义SCRIPT_FILENAME时使用了硬编码的方式。例如,fastcgi_param SCRIPT_FILENAME /var/www/foo$fastcgi_script_name。于是为了规范用法便引入了fastcgi.conf。
不过这样的话就产生一个疑问:为什么一定要引入一个新的配置文件,而不是修改旧的配置文件?这是因为fastcgi_param指令是数组型的,和普通指令相同的是:内层替换外层;和普通指令不同的是:当在同级多次使用的时候,是新增而不是替换。换句话说,如果在同级定义两次SCRIPT_FILENAME,那么它们都会被发送到后端,这可能会导致一些潜在的问题,为了避免此类情况,便引入了一个新的配置文件。
因此不再建议大家使用以下方式(搜了一下,网上大量的文章,并且nginx.conf的默认配置也是使用这种方式):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
而使用最新的方式:
include fastcgi.conf;
2.5.uwsgi_params
与fastcgi_params一样,传递哪些服务器变量,只有前缀不一样,以uwsgi_param开始而非fastcgi_param。
2.6.scgi_params
与fastcgi_params一样,传递哪些服务器变量,只有前缀不一样,以uwsgi_param开始而非fastcgi_param。
2.7.koi-utf、koi-win、win-utf
这三个文件都是与编码转换映射文件,用于在输出内容到客户端时,将一种编码转换到另一种编码。
koi-win: charset_map koi8-r < -- > windows-1251
koi-utf: charset_map koi8-r < -- > utf-8
win-utf: charset_map windows-1251 < -- > utf-8
koi8-r是斯拉夫文字8位元编码,供俄语及保加利亚语使用。在Unicode未流行之前,KOI8-R 是最为广泛使用的俄语编码,使用率甚至起ISO/IEC 8859-5还高。