Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1640663
  • 博文数量: 409
  • 博客积分: 6240
  • 博客等级: 准将
  • 技术积分: 4908
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-01 00:04
文章分类

全部博文(409)

文章存档

2021年(1)

2019年(1)

2017年(1)

2016年(13)

2015年(22)

2013年(4)

2012年(240)

2011年(127)

分类: LINUX

2012-03-07 13:51:48

没怎么用过nginx,之前管理员装的,我就自己研究下。nginx配置文件就200多行,相对容易点,不想Apache,1000多行,恶心死。。。
下载链接:wget
1.编译安装
默认安装到/usr/local/nginx
 
 tar -zxvf nginx-1.0.4.tar.gz 
cd nginx-1.0.4 
./configure 
make;make install
默认安装的路径是/usr/local/nginx
更多的安装配置 
./configure --prefix=/usr/local/nginx 
--with-openssl=/usr/include (启用ssl) 
--with-pcre=/usr/include/pcre/ (启用正规表达式) 
--with-http_stub_status_module (安装可以查看nginx状态的程序) 
--with-http_memcached_module (启用memcache缓存) 
--with-http_rewrite_module (启用支持url重写)
 
2.运行
 
 /usr/local/nginx/sbin/nginx 
#注:nginx默认使用80端口,若是80端口被占用,修改/usr/local/nginx/conf/nginx.conf里的 
server { 
listen 80;
 {启动命令:/usr/local/nginx/sbin/nginx       //启Nginx,不加参数的。}
3.测试
弄个图片之类的,43.jpg
4.基本命令

 /usr/local/nginx/sbin/nginx -h #帮助 
/usr/local/nginx/sbin/nginx -v #显示版本 
/usr/local/nginx/sbin/nginx -V #显示版本和配置信息 
/usr/local/nginx/sbin/nginx -t #测试配置 
/usr/local/nginx/sbin/nginx -q #测试配置时,只输出错误信息 
/usr/local/nginx/sbin/nginx -s stop #停止服务器 
/usr/local/nginx/sbin/nginx -s reload #重新加载配置 
/usr/local/nginx/sbin/nginx -s quit #不知道,估计和stop差不多 
/usr/local/nginx/sbin/nginx -s reopen #不知道,估计和reload类似 
/usr/local/nginx/sbin/nginx -p /nginx/path #默认为/usr/local/nginx(nginx安装路径),修改后影响log目录和html目录 
/usr/local/nginx/sbin/nginx -c /configure/file/path #配置文件路径,默认为conf/nginx.conf,有多个配置文件时很有用,用这个可以启动多个不同的nginx监听不同端口 
  1. 关闭nginx,没有stop命令:查询nginx主进程号
  2. ps -ef | grep nginx
  3. 在进程列表里 面找master进程,它的编号就是主进程号了。
  4. 步骤2:发送信号
  5. 从容停止Nginx:
  6. kill -QUIT 主进程号
  7. 快速停止Nginx:
  8. kill -TERM 主进程号
  9. 强制停止Nginx:
  10. pkill -9 nginx
  11. 另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
  12. kill -信号类型 '/usr/nginx/logs/nginx.pid'
  13. [yangkai@admin sbin]$ cat ../logs/nginx.pid 
  14. 2118
  15. [yangkai@admin sbin]$ ps -aux |grep nginx
  16. Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
  17. root      2118  0.0  0.0   6476   948 ?        Ss    2011   0:00 nginx: master process nginx
  18. www       3636  2.7  1.1  29592 24296 ?        S    00:01  35:24 nginx: worker process
  19. yangkai   6318  0.0  0.0   4888   692 pts/0    S+   21:40   0:00 grep nginx
  20. [yangkai@admin sbin]$ 
5.配置
配置文件在 安装目录/conf/nginx.conf
修改完后用/usr/local/nginx/sbin/nginx -s reload重新加载
 
 #user nobody; #nginx启动使用的用户,配置fastcgi时,需要改为有权限执行fastcgi的用户 
worker_processes 1; #nginx启动的进程数,1个已经足够了 
#error_log logs/error.log; #nginx 错误日志 相对于/usr/local/nginx/ 
#error_log logs/error.log notice; #nginx 记录警告日志 相对于/usr/local/nginx/ ,可改为logs/notice.log 
#error_log logs/error.log info; #nginx 记录信息日志 相对于/usr/local/nginx/ ,可改为logs/info.log 
#pid logs/nginx.pid; #nginx进程文件,最好不要改 
events { 
worker_connections 1024; #nginx最大连接数,最好小于系统的socket最大数和文件打开数 
http { 
include mime.types; #见同目录mime.types,用于根据文件后缀产生http header 
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"'; 
#access_log logs/access.log main; #访问日志 
sendfile on; 
#tcp_nopush on; 
#keepalive_timeout 0; 
keepalive_timeout 65; #保持连接时间,单位:秒 
#gzip on; 
server { 
listen 80; #监听的端口 
server_name localhost; #服务器名 
#charset koi8-r; #默认字符集 
#access_log logs/host.access.log main; #根据访问域名生成对应的访问日志 
location / { 
root html; #根目录,相对于安装目录 
index index.html index.htm; #默认主页 
#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
#} 
# 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; #转发到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 ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 
# ssl_prefer_server_ciphers on; 
# location / { 
# root html; 
# index index.html index.htm; 
# } 
#} 
}
6.Nginx负载均衡如何进行配置:http://developer.51cto.com/art/201003/190957.htm
--------------------------------------------------------------------------------------------
132nginx配置文件: nginx.rar   
  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. worker_rlimit_nofile 65535;
  8. events {
  9. use epoll;
  10. worker_connections 10240;
  11. }
  12. http {
  13. include mime.types;
  14. default_type application/octet-stream;
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. #access_log logs/access.log main;
  19. server_names_hash_bucket_size 128;
  20. client_header_buffer_size 2k;
  21. large_client_header_buffers 4 4k;
  22. client_max_body_size 8m;
  23. sendfile on;
  24. #tcp_nopush on;
  25. #keepalive_timeout 0;
  26. keepalive_timeout 60;
  27. #gzip on;
  28. upstream admin.m2m.cn {
  29. server 127.0.0.1:8082;
  30. }
  31. # upstream 61.#.132 {
  32. # server 61.#.132:80;
  33. # }
  34. upstream wow.ooaaoo.com {
  35. server 127.0.0.1:8091 weight=1;
  36. server 127.0.0.1:8092 weight=1;
  37. server 127.0.0.1:8096 weight=1;
  38. server sh.ooaaoo.com weight=1;
  39. }
  40. server {
  41. listen 80;
  42. server_name localhost;
  43. location / {
  44. root /ftpdata/update/;
  45. index index.html index.htm;
  46. }
  47. }
  48. server {
  49. listen 80;
  50. server_name admin.m2m.cn;
  51. # charset gb2312;
  52. # access_log logs/.access.log main;
  53. location / {
  54. # root /opt/nginx/html/nick;
  55. # index index.html index.htm;
  56. proxy_pass
  57. proxy_redirect off;
  58. proxy_set_header X-Real-IP $remote_addr;
  59. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  60. }
  61. location ~^/NginxStatus{
  62. stub_status on;
  63. access_log on;
  64. auth_basic "NginxStatus";
  65. #Auth_basic_user_file conf/htpasswd;
  66. }
  67. }
  68. # server {
  69. # listen 80;
  70. # server_name 61.#.132;
  71. # charset gb2312;
  72. # access_log logs/.access.log main;
  73. # location / {
  74. # root /opt/nginx/html/nick;
  75. # index index.html index.htm;
  76. # proxy_pass
  77. # proxy_redirect off;
  78. # proxy_set_header X-Real-IP $remote_addr;
  79. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  80. # }
  81. # }
  82. server {
  83. listen 80;
  84. server_name wow.ooaaoo.com;
  85. charset utf-8;
  86. access_log logs/resin.access.log main;
  87. location / {
  88. # root /opt/nginx/html/nick;
  89. # index index.html index.htm;
  90. proxy_pass
  91. proxy_redirect off;
  92. proxy_set_header X-Real-IP $remote_addr;
  93. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  94. proxy_set_header Host $http_hots;
  95. }
  96. }
  97. server {
  98. listen 80;
  99. server_name
  100. charset gb2312;
  101. access_log /ftpdata/update/logs/host.access.log main;
  102. location / {
  103. root /ftpdata/update/;
  104. index index.html index.htm;
  105. }
  106. #error_page 404 /404.html;
  107. # redirect server error pages to the static page /50x.html
  108. #
  109. error_page 500 502 503 504 /50x.html;
  110. location = /50x.html {
  111. root html;
  112. }
  113. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  114. #
  115. #location ~ \.php$ {
  116. # proxy_pass
  117. #}
  118. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  119. #
  120. #location ~ \.php$ {
  121. # root html;
  122. # fastcgi_pass 127.0.0.1:9000;
  123. # fastcgi_index index.php;
  124. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  125. # include fastcgi_params;
  126. #}
  127. # deny access to .htaccess files, if Apache's document root
  128. # concurs with nginx's one
  129. #
  130. #location ~ /\.ht {
  131. # deny all;
  132. #}
  133. }
  134. # another virtual host using mix of IP-, name-, and port-based configuration
  135. #
  136. #server {
  137. # listen 8000;
  138. # listen somename:8080;
  139. # server_name somename alias another.alias;
  140. # location / {
  141. # root html;
  142. # index index.html index.htm;
  143. # }
  144. #}
  145. # HTTPS server
  146. #
  147. #server {
  148. # listen 443;
  149. # server_name localhost;
  150. # ssl on;
  151. # ssl_certificate cert.pem;
  152. # ssl_certificate_key cert.key;
  153. # ssl_session_timeout 5m;
  154. # ssl_protocols SSLv2 SSLv3 TLSv1;
  155. # ssl_ciphers HIGH:!aNULL:!MD5;
  156. # ssl_prefer_server_ciphers on;
  157. # location / {
  158. # root html;
  159. # index index.html index.htm;
  160. # }
  161. #}
  162. }
#nginx官方链接:



阅读(2592) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~