Nginx安装与使用

摘要:一、安装Nginx:   1 :  wget下载: http://nginx.org/download/nginx-1.4.2.tar.gz  2 : 进行安装: tar -zxvf nginx-1.6.2.tar.gz 3 :  下载锁需要的依赖库文件:     yum install pcre     yum install pcre-devel     yum install

 

一、安装Nginx:

 

1 : wget下载: http://nginx.org/download/nginx-1.4.2.tar.gz

2 : 进行安装: tar -zxvf nginx-1.6.2.tar.gz

3 : 下载锁需要的依赖库文件:

yum install pcre

yum install pcre-devel

yum install zlib

yum install zlib-devel

1 (41).jpg

4 : 进行configure配置:cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx 查看是否报错

5 : 编译安装 make && make install

6 : 启动Nginx:

cd /usr/local/nginx目录下: 看到如下4个目录

....conf 配置文件

... html 网页文件

...logs 日志文件

...sbin 主要二进制程序

 

启动命令:/usr/local/nginx/sbin/nginx -s start 关闭(stop)重启(reload)

 

成功:查看是否启动(netstat -ano | grep 80)

失败:可能为80端口被占用等。

最终:

浏览器访问地址:http://192.168.1.172:80 (看到欢迎页面即可)

 

 

二、使用Nginx:简单与单台Tomcat整合 a) 首先找到nginx.conf文件:vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name localhost:80;

location / {

proxy_pass http://localhost:8080;

}

 

//...others

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

三、详细使用(nginx就是去配置其文件而已),如下所示: 1. #开启进程数<=CPU数 2. worker_processes1; 3. 4. #错误日志保存位置 5. #error_loglogs/error.log; 6. #error_loglogs/error.lognotice; 7. #error_loglogs/error.loginfo; 8. 9. #进程号保存文件 10. #pidlogs/nginx.pid; 11. 12. #等待事件 13. events{ 14. #每个进程最大连接数(最大连接=连接数x进程数) 15. #每个worker允许同时产生多少个链接,默认1024 16. worker_connections1024; 17. } 18. 19. 20. http{ 21. #文件扩展名与文件类型映射表 22. includemime.types; 23. #默认文件类型 24. default_typeapplication/octet-stream; 25. #日志文件输出格式这个位置相于全局设置 26. #log_formatmain\'$remote_addr-$remote_user[$time_local]"$request"\' 27. #\'$status$body_bytes_sent"$http_referer"\' 28. #\'"$http_user_agent""$http_x_forwarded_for"\'; 29. #请求日志保存位置 30. #access_loglogs/access.logmain; 31. #打开发送文件 32. sendfileon; 33. #tcp_nopushon; 34. #连接超时时间 35. #keepalive_timeout0; 36. keepalive_timeout65; 37. #打开gzip压缩 38. #gzipon; 39. #设定请求缓冲 40. client_header_buffer_size1k; 41. large_client_header_buffers44k; 42. #设定负载均衡的服务器列表 43. upstreammyproject{ 44. #weigth参数表示权值,权值越高被分配到的几率越大 45. #max_fails当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查 46. #fail_timeout在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器 47. #这里指定多个源服务器,ip:端口,80端口的话可写可不写 48. server192.168.1.78:8080weight=5max_fails=2fail_timeout=600s; 49. #server192.168.1.222:8080weight=3max_fails=2fail_timeout=600s; 50. } 51. 52. #第一个虚拟主机 53. server{ 54. #监听IP端口 55. listen80; 56. #主机名 57. server_namelocalhost; 58. #设置字符集 59. #charsetkoi8-r; 60. #本虚拟server的访问日志相当于局部变量 61. #access_loglogs/host.access.logmain; 62. #对本server"/"启用负载均衡 63. location/{ 64. #root/root;#定义服务器的默认网站根目录位置 65. #indexindex.phpindex.htmlindex.htm;#定义首页索引文件的名称 66. proxy_passhttp://myproject;#请求转向myproject定义的服务器列表 67. #以下是一些反向代理的配置可删除. 68. #proxy_redirectoff; 69. #proxy_set_headerHost$host; 70. #proxy_set_headerX-Real-IP$remote_addr; 71. #proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; 72. #client_max_body_size10m;#允许客户端请求的最大单文件字节数 73. #client_body_buffer_size128k;#缓冲区代理缓冲用户端请求的最大字节数, 74. #proxy_connect_timeout90;#nginx跟后端服务器连接超时时间(代理连接超时) 75. #proxy_send_timeout90;#后端服务器数据回传时间(代理发送超时) 76. #proxy_read_timeout90;#连接成功后,后端服务器响应时间(代理接收超时) 77. #proxy_buffer_size4k;#设置代理服务器(nginx)保存用户头信息的缓冲区大小 78. #proxy_buffers432k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 79. #proxy_busy_buffers_size64k;#高负荷下缓冲大小(proxy_buffers*2) 80. #proxy_temp_file_write_size64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传 81. } 82. location/upload{ 83. aliase:/upload; 84. } 85. #设定查看Nginx状态的地址 86. location/NginxStatus{ 87. stub_statuson; 88. access_logoff; 89. #allow192.168.0.3; 90. #denyall; 91. #auth_basic"NginxStatus"; 92. #auth_basic_user_fileconf/htpasswd; 93. } 94. #error_page404/404.html; 95. #redirectservererrorpagestothestaticpage/50x.html 96. #定义错误提示页面 97. error_page500502503504/50x.html; 98. location=/50x.html{ 99. roothtml; 100. } 101. #proxythePHPscriptstoApachelisteningon127.0.0.1:80 102. # 103. #location~.php${ 104. #proxy_passhttp://127.0.0.1; 105. #} 106. #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000 107. # 108. #location~.php${ 109. #roothtml; 110. #fastcgi_pass127.0.0.1:9000; 111. #fastcgi_indexindex.php; 112. #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; 113. #includefastcgi_params; 114. #} 115. #denyaccessto.htaccessfiles,ifApache\'sdocumentroot 116. #concurswithnginx\'sone 117. # 118. #location~/.ht{ 119. #denyall; 120. #} 121. } 122. 123. 124. #anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration 125. # 126. #server{ 127. #多监听 128. #listen8000; 129. #主机名 130. #listensomename:8080; 131. #server_namesomenamealiasanother.alias; 132. 133. #location/{ 134. #WEB文件路径 135. #roothtml; 136. #默认首页 137. #indexindex.htmlindex.htm; 138. #} 139. #} 140. 141. 142. #HTTPSserverHTTPSSSL加密服务器 143. # 144. #server{ 145. #listen443; 146. #server_namelocalhost; 147. 148. #sslon; 149. #ssl_certificatecert.pem; 150. #ssl_certificate_keycert.key; 151. 152. #ssl_session_timeout5m; 153. 154. #ssl_protocolsSSLv2SSLv3TLSv1; 155. #ssl_ciphersHIGH:!aNULL:!MD5; 156. #ssl_prefer_server_cipherson; 157. 158. #location/{ 159. #roothtml; 160. #indexindex.htmlindex.htm; 161. #} 162. #} 163. }

 

 

四.配置tomcat集群负载均衡(这里我们之间配置2个tomcat到nginx里,然后进行测试) 五.其他配置信息文件说明