utuntu上安装nginx

前言

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
  1. 获取pcre编译安装包     下载地址:http://download.csdn.net/download/liu976180578/10134160
  2. 解压缩pcre-xx.tar.gz包。
  3. 进入解压缩目录,执行./configure。
  4. make & make install
  5. 获取openssl编译安装包     下载地址:http://download.csdn.net/download/liu976180578/10134160
  6. 解压缩openssl-xx.tar.gz包。
  7. 进入解压缩目录,执行./config。
  8. make & make install
  9. 获取zlib编译安装包           下载地址:http://download.csdn.net/download/liu976180578/10134160
  10. 解压缩openssl-xx.tar.gz包。
  11. 进入解压缩目录,执行./configure。
  12. make & make install
  13. 获取nginx
  14. 解压缩nginx-xx.tar.gz包。  下载地址:http://download.csdn.net/download/liu976180578/10134160
  15. 进入解压缩目录,执行./configure
  16. 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可以验证是否安装启动成功。

Ubuntu 14.04下Nginx安装与使用 

Nginx的简单配置

  1. #Nginx创建进程所用用户和用户组  
  2. #user  nobody;  
  3. user  test  test;  
  4. #进程数,一般等于CPU数量  
  5. worker_processes  4;  
  6.   
  7.   
  8. #错误日志,定义到warn级别比较合适  
  9. #error_log  logs/error.log;  
  10. error_log  logs/error.log  warn;  
  11.   
  12.   
  13. #指定pid存放文件  
  14. pid        logs/nginx.pid;  
  15.   
  16. events {  
  17.     #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue  
  18.     use epoll;  
  19.   
  20.     #允许最大连接数  
  21.     worker_connections  1024;  
  22. }  
  23.   
  24. http {  
  25.     include       mime.types;  
  26.     default_type  application/octet-stream;  
  27.   
  28.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  29.     #                  '$status $body_bytes_sent "$http_referer" '  
  30.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  31.   
  32.     #关闭日志能够减少IO,但是建议开启error_log warn级别  
  33.     #access_log  logs/access.log  main;  
  34.   
  35.     sendfile        on;  
  36.     #tcp_nopush     on;  
  37.   
  38.     #HTTP连接的持续时间  
  39.     keepalive_timeout  30;  
  40.   
  41.     #压缩设置  
  42.     gzip  on;  
  43.     gzip_types  text/javascript text/plain text/css application/xml application/x-javascript;  
  44.   
  45.     #Tomcat集群配置  
  46.     upstream myserver {  
  47.       #通过client的IP进行映射  
  48.       ip_hash;  
  49.       # 负载均衡设置,weight越大越优先分配  
  50.       server  localhost:18080  weight=5;  
  51.       server  localhost:18081  weight=4;  
  52.     }  
  53.   
  54.     server {  
  55.         listen       8080; #监听的端口号  
  56.         server_name  localhost;  
  57.   
  58.         charset utf-8; #字符编码方式  
  59.   
  60.         #关闭日志能够减少IO  
  61.         #access_log  logs/host.access.log  main;  
  62.   
  63.         location / {  
  64.             root   html;  
  65.             index  index.html index.htm;  
  66.   
  67.   
  68.             #代理配置  
  69.             proxy_pass         http://myserver;  
  70.             #关闭重定向  
  71.             #proxy_redirect     off;  
  72.         }  
  73.   
  74.         #error_page  404              /404.html;  
  75.   
  76.         # redirect server error pages to the static page /50x.html  
  77.         #  
  78.         error_page   500 502 503 504  /50x.html;  
  79.         location = /50x.html {  
  80.             root   html;  
  81.         }  
  82.   
  83.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  84.         #  
  85.         #location ~ \.php$ {  
  86.         #    proxy_pass   http://127.0.0.1;  
  87.         #}  
  88.   
  89.   
  90.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  91.         #  
  92.         #location ~ \.php$ {  
  93.         #    root           html;  
  94.         #    fastcgi_pass   127.0.0.1:9000;  
  95.         #    fastcgi_index  index.php;  
  96.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  97.         #    include        fastcgi_params;  
  98.         #}  
  99.   
  100.   
  101.         # deny access to .htaccess files, if Apache's document root  
  102.         # concurs with nginx's one  
  103.         #  
  104.         #location ~ /\.ht {  
  105.         #    deny  all;  
  106.         #}  
  107.     }  
  108.   
  109.     # another virtual host using mix of IP-, name-, and port-based configuration  
  110.     #  
  111.     #server {  
  112.     #    listen       8000;  
  113.     #    listen       somename:8080;  
  114.     #    server_name  somename  alias  another.alias;  
  115.   
  116.   
  117.     #    location / {  
  118.     #        root   html;  
  119.     #        index  index.html index.htm;  
  120.     #    }  
  121.     #}  
  122.   
  123.   
  124.     # HTTPS server  
  125.     #  
  126.     #server {  
  127.     #    listen       443;  
  128.     #    server_name  localhost;  
  129.   
  130.   
  131.     #    ssl                  on;  
  132.     #    ssl_certificate      cert.pem;  
  133.     #    ssl_certificate_key  cert.key;  
  134.   
  135.   
  136.     #    ssl_session_timeout  5m;  
  137.   
  138.   
  139.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  140.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  141.     #    ssl_prefer_server_ciphers   on;  
  142.   
  143.   
  144.     #    location / {  
  145.     #        root   html;  
  146.     #        index  index.html index.htm;  
  147.     #    }  
  148.     #}  
  149. }  

一个机器上运行多个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>上面添加。



< Valve    className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"  host="192.168.145.130"  port="7000"  database="0"  maxInactiveInterval="60" /> 

host是redis的主机,port是redis的端口号   其他不用管,最好用redis集群

rides集群管理session
<ValveclassName="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<ManagerclassName="com.radiadesign.catalina.session.RedisSessionManager"
maxInactiveInterval="60"
sentinelMaster="mymaster"
sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" />

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还高。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章