nginx介绍以及编译安装

nginx:     功能:web服务,web代理 ,smtp反向代理

  
  
  
  
  1. yum install gcc openssl-devel pcre-devel zlib-devel  
  2.  
  3. groupadd nginx  
  4.  
  5. useradd –g nginx –s /bin/false –M nginx 
  
  
  
  
  1. # tar xvf nginx-1.1.3.tar.gz  
  2.  
  3. #cd nginx-1.1.3.tar.gz  
  4.  
  5. # ./configure \  
  6.  
  7.   --prefix=/usr \  
  8.  
  9.   --sbin-path=/usr/sbin/nginx \  
  10.  
  11.   --conf-path=/etc/nginx/nginx.conf \  
  12.  
  13.   --error-log-path=/var/log/nginx/error.log \  
  14.  
  15.   --http-log-path=/var/log/nginx/access.log \  
  16.  
  17.   --pid-path=/var/run/nginx/nginx.pid \  
  18.  
  19.   --lock-path=/var/lock/nginx.lock \  
  20.  
  21.   --user=nginx \  
  22.  
  23.   --group=nginx \  
  24.  
  25.   --with-http_ssl_module \  
  26.  
  27.   --with-http_flv_module \  
  28.  
  29.   --with-http_stub_status_module \  
  30.  
  31.   --with-http_gzip_static_module \  
  32.  
  33.   --http-client-body-temp-path=/var/tmp/nginx/client/ \  
  34.  
  35.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \  
  36.  
  37.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \  
  38.  
  39.   --with-pcre 支持Perl 

若要其他的可以用./configure –help

  
  
  
  
  1. #make   
  2.  
  3. #make install 

安装好后启动nginx,由于启动时没有脚本自己写放在/etc/init.d/:

  
  
  
  
  1. #!/bin/sh  
  2.  
  3. #  
  4.  
  5. # nginx - this script starts and stops the nginx daemon  
  6.  
  7. #  
  8.  
  9. # chkconfig: - 85 15   
  10.  
  11. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \  
  12.  
  13. # proxy and IMAP/POP3 proxy server  
  14.  
  15. # processname: nginx  
  16.  
  17. # config: /etc/nginx/nginx.conf  
  18.  
  19. # config: /etc/sysconfig/nginx  
  20.  
  21. # pidfile: /var/run/nginx.pid  
  22.  
  23.    
  24.  
  25. # Source function library.  
  26.  
  27. . /etc/rc.d/init.d/functions  
  28.  
  29.    
  30.  
  31. # Source networking configuration.  
  32.  
  33. . /etc/sysconfig/network  
  34.  
  35.    
  36.  
  37. Check that networking is up.  
  38.  
  39. "$NETWORKING" = "no" ] && exit 0  
  40.  
  41.    
  42.  
  43. nginx="/usr/sbin/nginx" 
  44.  
  45. prog=$(basename $nginx)  
  46.  
  47.    
  48.  
  49. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  50.  
  51.    
  52.  
  53. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  54.  
  55.    
  56.  
  57. lockfile=/var/lock/subsys/nginx  
  58.  
  59.    
  60.  
  61. make_dirs() {  
  62.  
  63.    # make required directories  
  64.  
  65.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`  
  66.  
  67.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`  
  68.  
  69.    for opt in $options; do  
  70.  
  71.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  72.  
  73.            value=`echo $opt | cut -d "=" -f 2`  
  74.  
  75.            if [ ! -d "$value" ]; then 
  76.  
  77.                # echo "creating" $value  
  78.  
  79.                mkdir -p $value && chown -R $user $value  
  80.  
  81.            fi  
  82.  
  83.        fi  
  84.  
  85.    done  
  86.  
  87. }  
  88.  
  89.    
  90.  
  91. start() {  
  92.  
  93.     [ -x $nginx ] || exit 5  
  94.  
  95.     [ -f $NGINX_CONF_FILE ] || exit 6  
  96.  
  97.     make_dirs  
  98.  
  99.     echo -n $"Starting $prog: " 
  100.  
  101.     daemon $nginx -c $NGINX_CONF_FILE  
  102.  
  103.     retval=$?  
  104.  
  105.     echo  
  106.  
  107.     [ $retval -eq 0 ] && touch $lockfile  
  108.  
  109.     return $retval  
  110.  
  111. }  
  112.  
  113.    
  114.  
  115. stop() {  
  116.  
  117.     echo -n $"Stopping $prog: " 
  118.  
  119.     killproc $prog -QUIT  
  120.  
  121.     retval=$?  
  122.  
  123.     echo  
  124.  
  125.     [ $retval -eq 0 ] && rm -f $lockfile  
  126.  
  127.     return $retval  
  128.  
  129. }  
  130.  
  131.    
  132.  
  133. restart() {  
  134.  
  135.     configtest || return $?  
  136.  
  137.     stop  
  138.  
  139.     sleep 1  
  140.  
  141.     start  
  142.  
  143. }  
  144.  
  145.    
  146.  
  147. reload() {  
  148.  
  149.     configtest || return $?  
  150.  
  151.     echo -n $"Reloading $prog: " 
  152.  
  153.     killproc $nginx -HUP  
  154.  
  155.     RETVAL=$?  
  156.  
  157.     echo  
  158.  
  159. }  
  160.  
  161.    
  162.  
  163. force_reload() {  
  164.  
  165.     restart  
  166.  
  167. }  
  168.  
  169.    
  170.  
  171. configtest() {  
  172.  
  173.   $nginx -t -c $NGINX_CONF_FILE  
  174.  
  175. }  
  176.  
  177.    
  178.  
  179. rh_status() {  
  180.  
  181.     status $prog  
  182.  
  183. }  
  184.  
  185.    
  186.  
  187. rh_status_q() {  
  188.  
  189.     rh_status >/dev/null 2>&1  
  190.  
  191. }  
  192.  
  193.    
  194.  
  195. case "$1" in 
  196.  
  197.     start)  
  198.  
  199.         rh_status_q && exit 0  
  200.  
  201.         $1  
  202.  
  203.         ;;  
  204.  
  205.     stop)  
  206.  
  207.         rh_status_q || exit 0  
  208.  
  209.         $1  
  210.  
  211.         ;;  
  212.  
  213.     restart|configtest)  
  214.  
  215.         $1  
  216.  
  217.         ;;  
  218.  
  219.     reload)  
  220.  
  221.         rh_status_q || exit 7  
  222.  
  223.         $1  
  224.  
  225.         ;;  
  226.  
  227.     force-reload)  
  228.  
  229.         force_reload  
  230.  
  231.         ;;  
  232.  
  233.     status)  
  234.  
  235.         rh_status  
  236.  
  237.         ;;  
  238.  
  239.     condrestart|try-restart)  
  240.  
  241.         rh_status_q || exit 0  
  242.  
  243.             ;;  
  244.  
  245.     *)  
  246.  
  247.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  248.  
  249.         exit 2  
  250.  
  251. esac 

保存设置执行权限

  
  
  
  
  1. #chown +x /etc/init.d/ngixd 
  
  
  
  
  1. 配置文件介绍:  
  2.  
  3.  
  4.  
  5. #user nobody;  
  6. worker_processes 1; CPU核数有关,能增强并行处理能力  
  7.  
  8. #error_log logs/error.log; 错误日志  
  9. #error_log logs/error.log notice; 不同级别的错误日志  
  10. #error_log logs/error.log info;  
  11.  
  12. #pid logs/nginx.pid;   
  13.  
  14.  
  15. events { 允许并发连接数  
  16. worker_connections 1024; 最大连接数  
  17. }  
  18.  
  19.  
  20. http {  
  21. include mime.types;   
  22. default_type application/octet-stream;  
  23.  
  24. #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
  25. '$status $body_bytes_sent "$http_referer" ' 
  26. '"$http_user_agent" "$http_x_forwarded_for"';  
  27.  
  28. #access_log logs/access.log main;  
  29.  
  30. sendfile on; 针对小文件访问,有良好的速率  
  31. #tcp_nopush on;  
  32.  
  33. #keepalive_timeout 0;  
  34. keepalive_timeout 65; 超时时间  
  35.  
  36. #gzip on; 压缩传输  
  37.  
  38. server { 单独定义主机,也可以定义虚拟主机  
  39. listen 80;  
  40. server_name localhost; 主机名  
  41.  
  42. #charset koi8-r;  
  43.  
  44. #access_log logs/host.access.log main;  
  45.  
  46. location / { 定义在哪个目录设置访问匹配权限  
  47. root html; 网页在html子目录下  
  48. index index.html index.htm;  
  49. }  
  50.  
  51. #error_page 404 /404.html;   
  52.  
  53. # redirect server error pages to the static page /50x.html  
  54. #  
  55. error_page 500 502 503 504 /50x.html;  
  56. location = /50x.html {  
  57. root html;  
  58. }  
  59.  
  60. # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  61. #  
  62. #location ~ \.php$ {  
  63. # proxy_pass http://127.0.0.1;  
  64. #}  
  65.  
  66. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  67. #  
  68. #location ~ \.php$ {  
  69. # root html;  
  70. # fastcgi_pass 127.0.0.1:9000;  
  71. # fastcgi_index index.php;  
  72. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  
  73. # include fastcgi_params;  
  74. #}  
  75.  
  76. # deny access to .htaccess files, if Apache's document root  
  77. # concurs with nginx's one  
  78. #  
  79. #location ~ /\.ht {  
  80. # deny all;  
  81. #}  
  82. }  
  83.  
  84.  
  85. # another virtual host using mix of IP-, name-, and port-based configuration  
  86. #  
  87. #server {  
  88. # listen 8000;  
  89. # listen somename:8080;  
  90. # server_name somename alias another.alias;  
  91.  
  92. # location / {  
  93. # root html;  
  94. index index.html index.htm;  
  95. # }  
  96. #}  
  97.  
  98.  
  99. # HTTPS server  
  100. #  
  101. #server {  
  102. # listen 443;  
  103. # server_name localhost;  
  104.  
  105. # ssl on;  
  106. # ssl_certificate cert.pem;  
  107. # ssl_certificate_key cert.key;  
  108.  
  109. # ssl_session_timeout 5m;  
  110.  
  111. # ssl_protocols SSLv2 SSLv3 TLSv1;  
  112. # ssl_ciphers HIGH:!aNULL:!MD5;  
  113. # ssl_prefer_server_ciphers on;  
  114.  
  115. # location / {  
  116. # root html;  
  117. index index.html index.htm;  
  118. # }  
  119. #}  
  120.  

介绍一些其他的upstream反向代理

  
  
  
  
  1. upstream name {  
  2.  
  3.                  server IP:80 weight=3;  
  4.  
  5.                  server IP:80 ;  
  6.  
  7.                 } 

location 某个网页位置中的某个文件的访问权限

      location [ =|~|~*|^~|@]

          =表示精确匹配

          不带任何符号模糊匹配

          ~ 区分大小写的正则表达式

         ~* 不区分大小写正则表达式

          ^~ 禁用正则表达式

      对于特定子目录的优先级高

What is FastCGIFastCGI

    is a high-speed and scalable interface for communicating with the web server scripting language. FastCGI is supported by many scripting languages, including php, if it is compiled with the option - --enable-fastcgi. It is supported by most popular web servers, including Apache (mod_fastcgi and mod_fcgid), Zeus, nginx and lighttpd. The main advantage of FastCGI is isolating the dynamic language from the web server. The technology, among other things, allows you to run a web server and dynamic language for the different hosts, which improves scalability and also aids security without a significant loss of productivity.PHP-FPM works on with any web server that supports FastCGI.

启动服务:service ngixd start 其中ngixd是编写的脚步的名字

  
  
  
  
  1. [root@squid2 nginx]# netstat -ntulp |grep nginx  
  2.  
  3.   tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12713/nginx.conf 

压力测试

 

你可能感兴趣的:(代理,Web,压力测试,ngixd)