nginx 安装

一、准备工作

1.关闭防火墙

  1. service iptables stop (关闭防火墙)(关闭selinux

2.安装一些包

  1. yum -y install zlib-devel pcre-devel openssl-devel openssl

3.创建nginx执行的用户(evans),如果存在忽略

  1. groupadd -r evans
  2. useradd -g evans evans

二、开始安装

1.移动nginx源码包到linux指定的用户编译源码目录

  1. mv nginx-1.6.0.tar.gz /usr/src/
  2. cd /usr/src/
  3. tar -zxvf nginx-1.6.0.tar.gz
  4. cd nginx-1.6.0

2.编译

  1. ./configure --prefix=/usr/local/nginx-1.6.0 --with-openssl=/usr/include/openssl --with-pcre --with-http_stub_status_module
  2. make
  3. make install

3.安装目录、已经启动项

  1. (安装成功在/usr/local/nginx-1.6.0 目录中)
  2. #配置文件目录
  3. /usr/local/nginx-1.6.0/conf/nginx.conf
  4. #命令
  5. /usr/local/nginx-1.6.0/sbin/nginx 启动
  6. /usr/local/nginx-1.6.0/sbin/nginx -s stop 快速关闭
  7. /usr/local/nginx-1.6.0/sbin/nginx -s quit 退出(关闭),这个会等待Nginx处理完当前请求
  8. /usr/local/nginx-1.6.0/sbin/nginx -s reload 重新加载配置文件
  9. /usr/local/nginx-1.6.0/sbin/nginx -s reopen 重新打开日志文件。
  10. #添加软链
  11. ln -s /usr/local/nginx-1.6.0/sbin/nginx /usr/local/bin/
  12. #配置文件目录软链
  13. ln -s /usr/local/nginx-1.6.0/conf/ /etc/nginx

四、配置文件详解

  1. vim /usr/local/nginx-1.6.0/conf/nginx.conf
  2. 1、修改配置文件
  3. #配置用户和用户组
  4. user evans evans;
  5. #工作进程数,建议设置为CPU的总核数
  6. worker_processes 2;
  7. #全局错误日志定义类型,日志等级从低到高依次为: debug | info | notice | warn | error | crit
  8. error_log logs/error.log info;
  9. #记录主进程ID的文件(不要动)
  10. #pid /usr/local/nginx-1.6.0/nginx.pid;
  11. #一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。但是由于nginx负载并不是完全均衡的,
  12. #所以这个值最好等于最多能打开的文件数。执行 sysctl -a | grep fs.file 可以看到linux文件描述符。
  13. worker_rlimit_nofile 65535;
  14. #工作模式与连接数上限
  15. events {
  16. #uname -a 工作模式,linux2.6版本以上用epoll
  17. use epoll;
  18. #单个进程允许的最大连接数
  19. worker_connections 65535;
  20. }
  21. 2、负载均衡和反向代理设置
  22. #设定http服务器,利用它的反向代理功能提供负载均衡支持
  23. http {
  24. #文件扩展名与文件类型映射表
  25. include mime.types;
  26. #最大上传
  27. client_max_body_size 20m;
  28. #默认文件类型
  29. default_type application/octet-stream;
  30. #日志格式
  31. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  32. '$status $body_bytes_sent "$http_referer" '
  33. '"$http_user_agent" "$http_x_forwarded_for"';
  34. #access log 记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息
  35. access_log logs/access.log main;
  36. #服务器名字的hash表大小
  37. server_names_hash_bucket_size 128;
  38. #客户端请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,
  39. #如果header过大,它会使用large_client_header_buffers来读取。
  40. #如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request
  41. #如果超过buffer,就会报HTTP 414错误(URI Too Long)
  42. #nginx接受最长的HTTP头部大小必须比其中一个buffer大,否则就会报400的HTTP错误(Bad Request)。
  43. client_header_buffer_size 32k;
  44. large_client_header_buffers 4 32k;
  45. #客户端请求体的大小
  46. client_body_buffer_size 8m;
  47. #隐藏ngnix版本号
  48. server_tokens off;
  49. #忽略不合法的请求头
  50. ignore_invalid_headers on;
  51. #指定启用除第一条error_page指令以外其他的error_page。
  52. recursive_error_pages on;
  53. #让 nginx 在处理自己内部重定向时不默认使用 server_name 设置中的第一个域名
  54. server_name_in_redirect off;
  55. #开启文件传输,一般应用都应设置为on;若是有下载的应用,则可以设置成off来平衡网络I/O和磁盘的I/O来降低系统负载
  56. sendfile on;
  57. #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。
  58. tcp_nopush on;
  59. #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,
  60. #这样发送一小块数据信息时就不能立即得到返回值。
  61. tcp_nodelay on;
  62. #长连接超时时间,单位是秒
  63. keepalive_timeout 65;
  64. #gzip模块设置,使用 gzip 压缩可以降低网站带宽消耗,同时提升访问速度。
  65. gzip on; #开启gzip
  66. gzip_min_length 1k; #最小压缩大小
  67. gzip_buffers 4 16k; #压缩缓冲区
  68. gzip_http_version 1.0; #压缩版本
  69. gzip_comp_level 2; #压缩等级
  70. gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型
  71. #upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.
  72. #weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
  73. upstream hostname {
  74. server 192.168.2.149:8080 max_fails=0 weight=1;
  75. server 192.168.1.9:8080 max_fails=0 weight=1;
  76. }
  77. #如果有拓展文件则添加 mkdir -p /usr/local/nginx-1.6.0/conf/conf.d
  78. include conf.d/*.conf; (放在跟nginx conf目录中)
  79. #三、主机配置模板
  80. server {
  81. #监听端口
  82. listen 80;
  83. #域名
  84. server_name hostname;
  85. #字符集
  86. charset utf-8;
  87. #单独的access_log文件
  88. access_log logs/192.168.2.149.access.log main;
  89. #反向代理配置,将所有请求为http://hostname的请求全部转发到upstream中定义的目标服务器中。
  90. location / {
  91. #此处配置的域名必须与upstream的域名一致,才能转发。
  92. proxy_pass http://hostname;
  93. proxy_set_header X-Real-IP $remote_addr;
  94. }
  95. #启用nginx status 监听页面
  96. location /nginxstatus {
  97. stub_status on;
  98. access_log on;
  99. }
  100. #错误页面
  101. error_page 500 502 503 504 /50x.html;
  102. location = /50x.html {
  103. root html;
  104. }
  105. }
  106. #php 可以直接用的demo
  107. server {
  108. listen 80;
  109. #网站别名
  110. server_name demo.box.cn;
  111. #access_log /data/logs/nginx/test.ttlsa.com.access.log main;
  112. #默认文件
  113. index index.php index.html;
  114. #网站跟目录
  115. root /web/www/demo;
  116. location / {
  117. try_files $uri $uri/ /index.php?$args;
  118. }
  119. location ~ .*\.(php)?$ {
  120. expires -1s;
  121. try_files $uri =404;
  122. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  123. include fastcgi_params;
  124. fastcgi_param PATH_INFO $fastcgi_path_info;
  125. fastcgi_index index.php;
  126. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  127. fastcgi_pass 127.0.0.1:9000;
  128. #fastcgi_pass unix:/usr/local/php5.5/var/run/php-fpm.pid;
  129. }
  130. }
  131. #php 直接使用的动态主机
  132. server {
  133. listen 80;
  134. #网站别名
  135. #access_log /data/logs/nginx/test.ttlsa.com.access.log main;
  136. #默认文件
  137. index index.php index.html index.html;
  138. #网站跟目录
  139. server_name ~^(?<app>.+)\.hadoop\.box\.com;
  140. root /web/www/$app;
  141. location / {
  142. try_files $uri $uri/ /index.php?$args;
  143. #try_files $uri $uri/ /public/index.php?$query_string;
  144. #try_files $uri $uri/ /index.php?s=$request_uri;
  145. }
  146. location ~ .*\.(php)?$ {
  147. expires -1s;
  148. try_files $uri =404;
  149. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  150. include fastcgi_params;
  151. fastcgi_param PATH_INFO $fastcgi_path_info;
  152. fastcgi_index index.php;
  153. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  154. #fastcgi_param SCRIPT_FILENAME /home/hadoop/app/uba/scripts/service/uba.php;
  155. fastcgi_pass 127.0.0.1:9000;
  156. #fastcgi_pass unix:/usr/local/php5.5/var/run/php-fpm.pid;
  157. #fastcgi_pass unix:/var/run/php5-fpm.sock;
  158. }
  159. }
  160. # nginx 转发到独立的 php 服务器 (待测试)
  161. upstream php-backend {
  162. server app0:9000;
  163. server app1:9000;
  164. server app2:9000;
  165. }
  166. location @php {
  167. include fastcgi_params;
  168. # 重写不会改变$document_uri,
  169. # 我们需要重写REQUEST_URI变量,否则laravel无法路由请求
  170. fastcgi_param REQUEST_URI $document_uri?$query_string;
  171. # 此处可以读取之前设置的$app_root变量,却无法动态设置root
  172. fastcgi_param SCRIPT_FILENAME $app_root/index.php;
  173. fastcgi_pass php-backend;
  174. }
  175. }
  176. #详细说明
  177. query_string = s=/Admin/User/personal.html
  178. request_uri = /Admin/User/personal.html
  179. $args | 请求中的参数;
  180. $binary_remote_addr | 远程地址的二进制表示
  181. $body_bytes_sent | 已发送的消息体字节数
  182. $content_length | HTTP请求信息里的"Content-Length";
  183. $content_type | 请求信息里的"Content-Type";
  184. $document_root | 针对当前请求的根路径设置值;
  185. $document_uri | $uri相同;
  186. $host | 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
  187. $hostname |
  188. $http_cookie | cookie 信息
  189. $http_post |
  190. $http_referer | 引用地址
  191. $http_user_agent | 客户端代理信息
  192. $http_via | 最后一个访问服务器的Ip地址。
  193. $http_x_forwarded_for | 相当于网络访问路径。
  194. $is_args |
  195. $limit_rate | 对连接速率的限制;
  196. $nginx_version |
  197. $pid |
  198. $query_string | $args相同;
  199. $realpath_root |
  200. $remote_addr | 客户端地址;
  201. $remote_port | 客户端端口号;
  202. $remote_user | 客户端用户名,认证用;
  203. $request | 用户请求
  204. $request_body |
  205. $request_body_file | 发往后端的本地文件名称
  206. $request_completion |
  207. $request_filename | 当前请求的文件路径名
  208. $request_method | 请求的方法,比如"GET""POST"等;
  209. $request_uri | 请求的URI,带参数;
  210. $scheme | 所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect;
  211. $sent_http_cache_control |
  212. $sent_http_connection |
  213. $sent_http_content_length |
  214. $sent_http_content_type |
  215. $sent_http_keep_alive |
  216. $sent_http_last_modified |
  217. $sent_http_location |
  218. $sent_http_transfer_encoding |
  219. $server_addr | 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
  220. $server_name | 请求到达的服务器名;
  221. $server_port | 请求到达的服务器端口号;
  222. $server_protocol | 请求的协议版本,"HTTP/1.0""HTTP/1.1";
  223. $uri | 请求的URI,可能和最初的值有不同,比如经过重定向之类的。