T086ѧϰ�� | վ��ѧԺ | �����ĵ� | ���� | Ъ���� | IP��ַ��ѯ | �������� | ���ջ��� | �����ֵ� | ���ù��� | ������Ϣ��ѯ

ʹ�� Nginx ������վ�����ٶȡ�����������

�� �������ߣ����� ����ʱ�䣺2010-01-27 | ���壺�� �� С��
[����]Nginx ���� Nginx ( engine x ) ��һ�������ܵ� HTTP �� �������� ��������Ҳ��һ�� IMAP/POP3/SMTP ������������ Nginx ���� Igor Sysoev Ϊ����˹�������ڶ��� Rambler.ru վ�㿪���ģ����Ѿ��ڸ�վ�����г�������...

Nginx ����

Nginx ("engine x") ��һ�������ܵ� HTTP �� �������� ��������Ҳ��һ�� IMAP/POP3/SMTP ������������ Nginx ���� Igor Sysoev Ϊ����˹�������ڶ��� Rambler.ru վ�㿪���ģ����Ѿ��ڸ�վ�����г����������ˡ� Igor ��Դ�������� BSD ����֤����ʽ���������ܻ��Dz��԰棬���ǣ�Nginx �Ѿ���Ϊ�����ȶ��ԡ��ḻ�Ĺ��ܼ���ʾ�������ļ��͵�ϵͳ��Դ�����Ķ������ˡ�

��������һ�ڣ�08 �� 6 �·ݣ��� NetCraft ���鱨����ʾ���Ѿ��г���������������ʹ���� Nginx���������ֳ���������һ���������� HTTP ������ lighttpd�� �������ģ����ҷ�չѸ�١����������ݱ�����ǰ�����ı�����

��Ʒ ��վ��
Apache 84,309,103
IIS 60,987,087
Google GFE 10,465,178
Unknown 4,903,174
nginx 2,125,160
Oversee 1,953,848
lighttpd 1,532,952

�������ڵ��鱨���ĸ���ϸ��Ϣ�뿴�������ӣ�

http://survey.netcraft.com/Reports/200806/

ʹ�� Nginx ǰ�����˽�������

  1. Ŀǰ�ٷ� Nginx ����֧�� Windows����ֻ���ڰ��� Linux��UNIX��BSD ϵͳ�°�װ��ʹ�ã�
  2. Nginx ����ֻ��һ�� HTTP �ͷ������������������޷��� Apache һ��ͨ����װ����ģ����֧�ֲ�ͬ��ҳ���ű������� PHP��CGI �ȣ�
  3. Nginx ֧�ּ򵥵ĸ��ؾ������ݴ���
  4. ֧����Ϊ���� HTTP �������Ĺ��ܣ�������־��ѹ����Byte ranges��Chunked responses��SSL�����������ȵȣ�Ӧ�о��С�

�� Linux �°�װ Nginx

Ϊ��ȷ������ Nginx ��ʹ����������ʽ���и����������ã���װ֮ǰ��Ҫȷ��ϵͳ�Ƿ���װ�� PCRE��Perl Compatible Regular Expressions�����������Ե� ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ �������µ� PCRE Դ������ʹ�������������ر����Ͱ�װ PCRE ����

# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
# tar zxvf pcre-7.7.tar.gz
# cd pcre-7.7
# ./configure
# make
# make install
			

 

��������װ Nginx��Nginx һ���������汾���ֱ����ȶ����Ϳ����棬�����Ը�������Ŀ����ѡ���������汾������һ���������ǰ� Nginx ��װ�� /opt/nginx Ŀ¼�µ���ϸ���裺

# wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
# tar zxvf nginx-0.6.31.tar.gz
# cd nginx-0.6.31
# ./configure --with-http_stub_status_module �Cprefix=/opt/nginx
# make
# make install
			

 

���в��� --with-http_stub_status_module ��Ϊ������ nginx �� NginxStatus ���ܣ��������� Nginx �ĵ�ǰ״̬��

��װ�ɹ��� /opt/nginx Ŀ¼�����ĸ���Ŀ¼�ֱ��ǣ�conf��html��logs��sbin ������ Nginx �������ļ������� conf/nginx.conf��Nginx ֻ��һ�������ļ�λ�� sbin Ŀ¼�µ� nginx �ļ���ȷ��ϵͳ�� 80 �˿�û����������ռ�ã����� sbin/nginx ���������� Nginx���������������ʴ˻����� IP���������������� Welcome to nginx! ����ʾ Nginx �Ѿ���װ�����гɹ���

���õ� Nginx �����Ϳ���

���������

Nginx ��װ��ֻ��һ�������ļ������������ṩ���ֹ�������������ʹ�ò�����ϵͳ�źŻ��ƶ� Nginx ���̱������п��Ƶġ� Nginx �IJ������������¼�����

-c <path_to_config>��ʹ��ָ���������ļ������� conf Ŀ¼�µ� nginx.conf ��

-t �����������ļ��Ƿ���ȷ��������ʱ��Ҫ���¼������õ�ʱ�򣬴������dz���Ҫ�������������޸ĵ������ļ��Ƿ����﷨������

-v ����ʾ nginx �汾�š�

-V ����ʾ nginx �İ汾���Լ����뻷����Ϣ�Լ�����ʱ�IJ�����

��������Ҫ����ij�������ļ��Ƿ���д��ȷ�����ǿ���ʹ����������

sbin/nginx �C t �C c conf/nginx2.conf

 

ͨ���źŶ� Nginx ���п���

Nginx ֧���±��е��źţ�

��� ��������
TERM, INT ���ٹرճ�������ֹ��ǰ���ڴ���������
QUIT �����굱ǰ�����󣬹رճ���
HUP ���¼������ã��������µĹ������̣��رվ͵Ľ��̣��˲��������ж�����
USR1 ���´�����־�ļ��������л���־������ÿ������һ���µ���־�ļ�
USR2 ƽ��������ִ�г���
WINCH ���ݹرչ�������

 

�����ַ�ʽ��ͨ����Щ�ź�ȥ���� Nginx����һ��ͨ�� logs Ŀ¼�µ� nginx.pid �鿴��ǰ���е� Nginx �Ľ��� ID��ͨ�� kill �C XXX <pid> ������ Nginx������ XXX �����ϱ����г����ź�������������ϵͳ��ֻ��һ�� Nginx ���̣�����Ҳ����ͨ�� killall ���������ɣ��������� killall �C s HUP nginx ���� Nginx ���¼������á�

���� Nginx

������һ��ʵ�ʵ������ļ���

 user  nobody;# �������̵�����
 worker_processes  4;# ������������һ���� CPU ������ͬ

 #error_log  logs/error.log; 
 #error_log  logs/error.log  notice; 
 #error_log  logs/error.log  info; 

 #pid        logs/nginx.pid; 

 events { 
    use epoll;#Linux ���������õ� event ģʽ
    worker_connections  2048;# ÿ��������������������ͬʱ������
 } 

 http { 
    include       mime.types; 
    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  off; 
    access_log  logs/access.log;# ��־�ļ���

    sendfile        on; 
    #tcp_nopush     on; 
    tcp_nodelay     on; 

    keepalive_timeout  65; 

    include 	 gzip.conf; 
    
    # ��Ⱥ�е����к�̨��������������Ϣ
    upstream tomcats { 
	 server 192.168.0.11:8080 weight=10; 
	 server 192.168.0.11:8081 weight=10; 
	 server 192.168.0.12:8080 weight=10; 
	 server 192.168.0.12:8081 weight=10; 
	 server 192.168.0.13:8080 weight=10; 
	 server 192.168.0.13:8081 weight=10; 
    } 



    server { 
        listen       80;#HTTP �Ķ˿�
        server_name  localhost; 

        charset utf-8; 

        #access_log  logs/host.access.log  main; 

	 location ~ ^/NginxStatus/ { 
	    stub_status on; #Nginx ״̬��������
	    access_log off; 
	 } 

	 location ~ ^/(WEB-INF)/ { 
	    deny all; 
	 } 
	

	 location ~ .(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|
	 zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { 
             root /opt/webapp; 
	    expires 24h; 
        } 

        location / { 
	    proxy_pass http://tomcats;# ��������
	    include proxy.conf; 
        } 

        error_page 404 /html/404.html; 

        # redirect server error pages to the static page /50x.html 
        # 
	 error_page 502 503 /html/502.html; 
        error_page 500 504 /50x.html; 
        location = /50x.html { 
            root   html; 
        } 
    } 
 } 

 

Nginx ����

������һ��ʵ����վ������ʵ�������л�ɫ����Ϊ����˵�������������У��������Ƕ�����һ�� location ~ ^/NginxStatus/������ͨ�� http://localhost/NginxStatus/ �Ϳ��Լ��ص� Nginx ��������Ϣ����ʾ���������£�

Active connections: 70 
server accepts handled requests
 14553819 14553819 19239266 
Reading: 0 Writing: 3 Waiting: 67 
			

 

NginxStatus ��ʾ��������˼���£�

  • active connections �C ��ǰ Nginx �������Ļ��������
  • server accepts handled requests -- �ܹ������� 14553819 ������ , �ɹ����� 14553819 ������ ( ֤���м�û��ʧ�ܵ� ), �ܹ������� 19239266 ������ ( ƽ��ÿ�����ִ����� 1.3 ���������� )��
  • reading -- nginx ��ȡ���ͻ��˵� Header ��Ϣ����
  • writing -- nginx ���ظ��ͻ��˵� Header ��Ϣ����
  • waiting -- ���� keep-alive �������£�����ֵ���� active - (reading + writing)����˼���� Nginx �Ѿ����������ڵȺ���һ������ָ����פ�����ӡ�

��̬�ļ�����

ͨ����������ʽ�����ǿ��� Nginx ʶ�������־�̬�ļ������� images ·���µ�������������дΪ��

location ~ ^/images/ {
    root /opt/webapp/images;
}
			

 

�������������������˼����ļ����͵�����������ʽ��

location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
    root /opt/webapp;
    expires 24h;
}
			

 

��������ͼƬ����̬ HTML �ļ���js �ű��ļ��� css ��ʽ�ļ��ȣ�����ϣ�� Nginx ֱ�Ӵ��������ظ����������������Դ����ļӿ���ҳ����ʱ���ٶȡ����˶��������ļ�������Ҫͨ�� root ָ����ָ���ļ��Ĵ���·����ͬʱ��Ϊ�����ļ��������޸ģ�ͨ�� expires ָ�������������������Ļ��棬�Լ��ٲ���Ҫ�������� expires ָ�����Կ��� HTTP Ӧ���еġ� Expires ���͡� Cache-Control ����ͷ�꣨�𵽿���ҳ�滺�������ã���������ʹ���������µĸ�ʽ����д Expires��

expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;
			

 

��̬ҳ����������

Nginx ��������֧���������е� JSP��ASP��PHP��PERL �ȶ�̬ҳ�棬����������ͨ�������������������͵����˵ķ����������� Tomcat��Apache��IIS �������ɶ�̬ҳ��������������ǰ��������ʾ���У��������ȶ������� Nginx ֱ�Ӵ�����һЩ��̬�ļ����������������е�����ͨ�� proxy_pass ָ��͸����˵ķ����� ���������������� Tomcat�������򵥵� proxy_pass �÷����£�

location / {
    proxy_pass        http://localhost:8080;
    proxy_set_header  X-Real-IP  $remote_addr;
}
			

 

��������û��ʹ�õ���Ⱥ�����ǽ�����ֱ���͵������� 8080 �˿ڵ� Tomcat ���������������� JSP �� Servlet ������������

��ҳ���ķ������dz�����ʱ����������Ҫ����Ӧ�÷���������ͬ�е���̬ҳ����ִ�в�������ʱ���Ǿ���Ҫʹ�ü�Ⱥ�ļܹ��� Nginx ͨ�� upstream ָ��������һ���������ļ�Ⱥ����ǰ���Ǹ����������������Ƕ�����һ����Ϊ tomcats �ļ�Ⱥ��������Ⱥ�а�������̨�������� 6 �� Tomcat ���񡣶� proxy_pass ָ����д�������ˣ�

location / {
    proxy_pass        http://tomcats;
    proxy_set_header  X-Real-IP  $remote_addr;
}
			

 

�� Nginx �ļ�Ⱥ�����У�Nginx ʹ�����򵥵�ƽ��������������Ⱥ�е�ÿ���ڵ�����������һ��ij���ڵ�ʧЧʱ������������Чʱ��Nginx �����dz���ʱ�Ĵ���״̬�ı仯���Ա�֤����Ӱ�쵽�û��ķ��ʡ�

�ܽ�

��������������ֻ�����ٶ� K������ȸ��С��������ȫ�� Nginx �ٷ��ṩ�ĸ��ֹ���ģ��Ӧ�о��У�������Щģ�������������ָ���������Ҫ�������磺ѹ��������������Ⱥ��FastCGI����ý���������� Memcached ֧�֡�URL ��д�ȵ� �����ؼ����� Nginx ӵ�� Apache ������ HTTP �������޷������ĸ����ܡ������������ڲ��ı�ԭ����վ�ļܹ��ϣ�ͨ����ǰ������ Nginx ��������վ�ķ����ٶȡ�

����ֻ�Ǽ򵥽����� Nginx �İ�װ�Լ������Ļ��������ú�ʹ�ã��������� Nginx ����Ϣ���Ķ����º����IJο���Դ��������Ҫ�dz���л�ҵ����ѡ������ڣ�[email protected]������һֱ���� Nginx ������ WIKI��http://wiki.codemongers.com/NginxChs ����ͬʱҲ�������ܸ�����ô�õ�һ��������

����������վ�������� Linux �£���������û��ʹ��һЩ�dz����ӵĶ���ȷ�� Nginx �޷����ɵĹ��ܣ�����Ӧ������ Nginx ��

  • ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
  • �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
������ѯ Health Check Gulangyu
© 2017 T086ѧϰ�� - T086.com(ԭitlearner.com)
RunTime:6.40ms QueryTime:7