欢迎大家赞助一杯啤酒🍺 我们准备了下酒菜:Formal mathematics/Isabelle/ML, Formal verification/Coq/ACL2, C++/F#/Lisp
Nginx
小 (→图集) |
|||
(未显示1个用户的46个中间版本) | |||
第1行: | 第1行: | ||
− | |||
{{SeeWikipedia}} | {{SeeWikipedia}} | ||
− | [[Image: | + | [[Image:nginx-logo.png|right]] |
+ | Nginx | ||
+ | |||
+ | ==简介== | ||
[[Image:netcraft_chart_with_nginx_and_lighttpd.png|right|thumb|[http://survey.netcraft.com/Reports/200806/ Nginx more than Lighttpd]]] | [[Image:netcraft_chart_with_nginx_and_lighttpd.png|right|thumb|[http://survey.netcraft.com/Reports/200806/ Nginx more than Lighttpd]]] | ||
nginx [engine x] is a HTTP server and mail proxy server. | nginx [engine x] is a HTTP server and mail proxy server. | ||
第12行: | 第14行: | ||
<rss>http://nginx.org/index.rss|short|date|max=10</rss> | <rss>http://nginx.org/index.rss|short|date|max=10</rss> | ||
− | == | + | ==功能== |
+ | |||
+ | ==指南== | ||
./configure | ./configure | ||
make | make | ||
第39行: | 第43行: | ||
./configure: error: the HTTP rewrite module requires the PCRE library. | ./configure: error: the HTTP rewrite module requires the PCRE library. | ||
apt-get install libpcre3-dev | apt-get install libpcre3-dev | ||
+ | 也可能需要 apt-get install zlib1g zlib1g-dev | ||
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. | ./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. | ||
apt-get install libssl-dev | apt-get install libssl-dev | ||
第46行: | 第51行: | ||
kill -HUP `cat /usr/local/nginx/logs/nginx.pid` | kill -HUP `cat /usr/local/nginx/logs/nginx.pid` | ||
*[http://blog.jiqila.com/post/223/ FreeBSD上搭建nginx 0.8.x + PHP 5.3.x(FastCGI) + MySQL 5.1.x] | *[http://blog.jiqila.com/post/223/ FreeBSD上搭建nginx 0.8.x + PHP 5.3.x(FastCGI) + MySQL 5.1.x] | ||
+ | 列出目录 | ||
+ | autoindex on; | ||
+ | autoindex_exact_size off; | ||
+ | autoindex_localtime on; | ||
+ | |||
+ | ==分发版== | ||
+ | *[[OpenResty]] | ||
+ | *[http://tengine.taobao.org/index_cn.html Tengine] | ||
+ | |||
+ | ==Java== | ||
+ | Nginx作为[[Jetty]], [[Apache Tomcat]]的前端 | ||
+ | |||
+ | *[https://www.nginx.com/resources/deployment-guides/load-balance-apache-tomcat/ Load Balancing Apache Tomcat with NGINX and NGINX Plus] | ||
+ | *[http://wiki.nginx.org/JavaServers servers like Jetty, GlassFish and Tomcat] | ||
+ | *[https://hi-nginx-java.hi-nginx.com/index.html hi-nginx-java] = nginx + java | ||
==Python== | ==Python== | ||
+ | *[[uWSGI]]: [http://library.linode.com/web-servers/nginx/python-uwsgi Python-uWSGI with Nginx] | ||
+ | |||
==PHP== | ==PHP== | ||
+ | *安装[[PHP-FPM]] | ||
+ | *配置nginx使其支持PHP | ||
+ | 修改 /usr/local/etc/nginx/nginx.conf | ||
+ | |||
+ | 修改 /usr/local/etc/nginx/fastcgi_params | ||
+ | |||
+ | 修改 //usr/local/etc/php-fpm.conf | ||
+ | |||
==Ruby== | ==Ruby== | ||
+ | |||
+ | ==Markdown== | ||
+ | 配置 Nginx 支持 [[Markdown]] 格式 | ||
+ | |||
==为什么选择Nginx== | ==为什么选择Nginx== | ||
Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性: | Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性: | ||
第110行: | 第144行: | ||
* speed limitation; | * speed limitation; | ||
* limitation of simultaneous connections from one address. | * limitation of simultaneous connections from one address. | ||
+ | ==Nginx + [[Jetty]]== | ||
+ | |||
==Nginx + [[GlassFish]]== | ==Nginx + [[GlassFish]]== | ||
upstream glassfish { | upstream glassfish { | ||
server 127.0.0.1:3000; | server 127.0.0.1:3000; | ||
} | } | ||
+ | |||
==Modules== | ==Modules== | ||
需要 ngx_http_autoindex_module 模块 | 需要 ngx_http_autoindex_module 模块 | ||
第122行: | 第159行: | ||
} | } | ||
− | == | + | ==站点== |
− | + | server | |
+ | { | ||
+ | listen 80; | ||
+ | server_name blog.huihoo.com; | ||
+ | index index.htm index.html index.php; | ||
+ | root /var/www/blog.huihoo.com; | ||
+ | location ~ \.php$ { | ||
+ | fastcgi_split_path_info ^(.+\.php)(/.+)$; | ||
+ | # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini | ||
+ | # With php5-cgi alone: | ||
+ | #fastcgi_pass 127.0.0.1:9000; | ||
+ | # With php5-fpm: | ||
+ | fastcgi_pass unix:/var/run/php5-fpm.sock; | ||
+ | fastcgi_index index.php; | ||
+ | include fastcgi_params; | ||
+ | client_max_body_size 30m; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | ==多站点== | ||
+ | server { # php/fastcgi | ||
+ | listen 80; | ||
+ | server_name domain1.com www.domain1.com; | ||
+ | access_log logs/domain1.access.log main; | ||
+ | root html; | ||
+ | |||
+ | location ~ \.php$ { | ||
+ | fastcgi_pass 127.0.0.1:1025; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | server { # simple reverse-proxy | ||
+ | listen 80; | ||
+ | server_name domain2.com www.domain2.com; | ||
+ | access_log logs/domain2.access.log main; | ||
+ | |||
+ | # serve static files | ||
+ | location ~ ^/(images|javascript|js|css|flash|media|static)/ { | ||
+ | root /var/www/virtual/big.server.com/htdocs; | ||
+ | expires 30d; | ||
+ | } | ||
+ | [https://www.nginx.com/resources/wiki/start/topics/examples/full/ 更完整的Nginx配置信息] | ||
+ | |||
+ | ==Rewrite== | ||
+ | Discuz!在Nginx下的Rewrite | ||
+ | rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last; | ||
+ | rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last; | ||
+ | rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last; | ||
+ | rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last; | ||
+ | rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last; | ||
+ | break; | ||
+ | |||
+ | ==技巧== | ||
+ | 没有html后缀,也能正常显示,如:http://docs.huihoo.com/go/golang.org/doc/go1.6 且类似 [http://docs.huihoo.com/go/golang.org/src/runtime/Makefile Makefile] [http://docs.huihoo.com/go/golang.org/src/all.bat .bat] [http://docs.huihoo.com/go/golang.org/src/all.bash .bash] [http://docs.huihoo.com/go/golang.org/src/all.rc .rc] [http://docs.huihoo.com/go/golang.org/src/runtime/asm.s .s] [http://docs.huihoo.com/go/golang.org/src/runtime/alg.go .go] 的文件都能显示。 | ||
+ | location / { | ||
+ | add_header Content-Type: 'text/html; charset=utf-8'; | ||
+ | 但会导致一些浏览器无法打开 docs.huihoo.com 网页会直接下载而不会被浏览,已暂时取消 add_header 设置。 | ||
+ | |||
+ | [http://nginx.org/en/docs/http/ngx_http_headers_module.html Module ngx_http_headers_module] | ||
+ | |||
+ | http://book.huihoo.com/practical-postgresql/ppbook/ 的问题还没解决 | ||
+ | |||
+ | 支持在线播放 avi, wmv | ||
+ | video/x-ms-wmv wmv; | ||
+ | video/x-msvideo avi; | ||
+ | |||
+ | ==图集== | ||
+ | [http://wiki.huihoo.com/images/8/89/Nginx-architecture.png Nginx架构] | ||
+ | <gallery> | ||
+ | image:nginx-architecture-high-level-overview.png|Nginx架构 | ||
+ | image:web-server-survey.png|Web Server Survey | ||
+ | </gallery> | ||
+ | |||
+ | ==链接== | ||
+ | *https://nginx.org/ | ||
+ | *[https://www.nginx.com/ Nginx官网] | ||
+ | *[https://github.com/nginx/nginx Nginx @ GitHub] | ||
*http://wiki.nginx.org | *http://wiki.nginx.org | ||
+ | *[https://github.com/fcambus/nginx-resources Nginx Resources] [[image:awesome.png]] | ||
+ | *[http://docs.huihoo.com/nginx/agentzh-nginx-tutorials-zh-cn.html agentzh 的 Nginx 教程] | ||
*http://sysoev.ru/en/ | *http://sysoev.ru/en/ | ||
*http://wiki.codemongers.com/ | *http://wiki.codemongers.com/ | ||
− | * | + | *[http://code.google.com/p/nginxsrp/wiki/NginxCodeReview Ningx代码研究] |
− | * | + | *[http://wiki.codemongers.com/NginxChs Nginx 的中文维基] |
*http://download.huihoo.com/nginx/ | *http://download.huihoo.com/nginx/ | ||
*http://docs.huihoo.com/nginx/ | *http://docs.huihoo.com/nginx/ | ||
+ | *[http://oneoo.com/articles/nginx-php-grid.html 使用 Nginx 构建一个“高”可用的 PHP 集群] | ||
+ | *[http://docs.huihoo.com/nginx/zh-cn/ Nginx开发从入门到精通] | ||
+ | *[https://www.nginx.com/blog/benefits-of-microcaching-nginx/ The Benefits of Microcaching with NGINX] | ||
− | + | [[category:web server]] | |
− | + | [[category:nginx]] | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | [[ | + | |
− | [[ | + |
2022年4月12日 (二) 08:25的最后版本
您可以在Wikipedia上了解到此条目的英文信息 Nginx Thanks, Wikipedia. |
Nginx
目录 |
[编辑] 简介
nginx [engine x] is a HTTP server and mail proxy server.
Nginx is #8 on January 2008 Netcraft survey with more than 842,000 domains using it! (8x since last year) (05 Jan 2008)
[编辑] 新闻
- njs-0.8.7 bugfix version has been released. (21 Oct 2024 21:00)
- nginx-1.27.2 mainline version has been released. (01 Oct 2024 21:00)
- njs-0.8.6 version has been released, featuring the QuickJS engine support. (01 Oct 2024 21:00)
- The nginx project has officially moved to GitHub. Use GitHub issues instead of trac.nginx.org, GitHub discussions instead of nginx mailing list. (05 Sep 2024 21:00)
- nginx-1.26.2 stable and nginx-1.27.1 mainline versions have been released, with a fix for the buffer overread vulnerability in the ngx_http_mp4_module (CVE-2024-7347). (13 Aug 2024 21:00)
- njs-0.8.5 version has been released. (24 Jun 2024 21:00)
- The njs project has officially moved to GitHub. Read more. (04 Jun 2024 21:00)
- nginx-1.26.1 stable and nginx-1.27.0 mainline versions have been released, with fixes for vulnerabilities in HTTP/3 (CVE-2024-32760, CVE-2024-31079, CVE-2024-35200, CVE-2024-34161). (28 May 2024 21:00)
- The nginx.org website project has officially moved to GitHub. (12 May 2024 21:00)
- nginx-1.26.0
stable version has been released,
incorporating new features and bug fixes from the 1.25.x mainline branch —
including experimental
HTTP/3 support,
HTTP/2 on a
per-server basis, virtual servers in the stream module, passing stream connections to listen sockets,
and more. (22 Apr 2024 21:00)
[编辑] 功能
[编辑] 指南
./configure make sudo make install
一个参考
./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_gzip_static_module \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
more detail: http://wiki.nginx.org/NginxModules
可能的问题: ./configure: error: the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3-dev
也可能需要 apt-get install zlib1g zlib1g-dev ./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.
apt-get install libssl-dev
Start
# /usr/local/nginx/sbin/nginx // 配置文件: usr/local/nginx/conf/nginx.conf
reload the configuration with
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
列出目录
autoindex on; autoindex_exact_size off; autoindex_localtime on;
[编辑] 分发版
[编辑] Java
Nginx作为Jetty, Apache Tomcat的前端
- Load Balancing Apache Tomcat with NGINX and NGINX Plus
- servers like Jetty, GlassFish and Tomcat
- hi-nginx-java = nginx + java
[编辑] Python
[编辑] PHP
- 安装PHP-FPM
- 配置nginx使其支持PHP
修改 /usr/local/etc/nginx/nginx.conf
修改 /usr/local/etc/nginx/fastcgi_params
修改 //usr/local/etc/php-fpm.conf
[编辑] Ruby
[编辑] Markdown
配置 Nginx 支持 Markdown 格式
[编辑] 为什么选择Nginx
Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:
- 在高连接并发的情况下,Nginx是Apache服务器不错的替代品: Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue作为开发模型. Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(FreeBSD)网络I/O模型,而Apache则使用的是传统的Select模型. 目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型.
- Nginx作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多.
- 作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
- Nginx 是一个 安装非常的简单 , 配置文件 非常简洁(还能够支持perl语法), Bugs非常少的服务器: Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够 不间断服务的情况下进行软件版本的升级.
参考:
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)
- Nginx 0.8.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器(第5版)
- Nginx 0.7.x + PHP 5.2.8(FastCGI)搭建胜过Apache十倍的Web服务器(第4版)
- Nginx 0.6.31 + PHP 5.2.6(FastCGI)搭建胜过Apache十倍的Web服务器(第3版)
- Nginx 0.5.33 + PHP 5.2.5(FastCGI)搭建胜过Apache 10倍的Web服务器(第2版)
- Nginx 0.5.31 + PHP 5.2.4(FastCGI)搭建可承受3万以上并发连接数,胜过Apache 10倍的Web服务器(第1版)
[编辑] Hash表
Ngnix使用hash表来协助完成请求的快速处理。
考虑到保存键及其值的hash表存储单元的大小不至于超出设定参数(hash bucket size), 在启动和每次重新配置时,Nginx为hash表选择尽可能小的尺寸。
直到hash表超过参数(hash max size)的大小才重新进行选择. 对于大多数hash表都有指令来修改这些参数。例如,保存服务器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果 hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键值。因此,如果Nginx给出需要增大 hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
[编辑] 事件模型
Nginx支持如下处理连接的方法(I/O复用方法),这些方法可以通过use指令指定。
- select - 标准方法。 如果当前平台没有更有效的方法,它是编译时默认的方法。你可以使用配置参数 --with-select_module 和 --without-select_module 来启用或禁用这个模块。
- poll - 标准方法。 如果当前平台没有更有效的方法,它是编译时默认的方法。你可以使用配置参数 --with-poll_module 和 --without-poll_module 来启用或禁用这个模块。
- kqueue - 高效的方法,使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
- epoll - 高效的方法,使用于Linux内核2.6版本及以后的系统。在某些发行版本中,如SuSE 8.2, 有让2.4版本的内核支持epoll的补丁。
- rtsig - 可执行的实时信号,使用于Linux内核版本2.2.19以后的系统。默认情况下整个系统中不能出现大于1024个POSIX实时(排队)信号。这种情况对于高负载的服务器来说是低效的;所以有必要通过调节内核参数 /proc/sys/kernel/rtsig-max 来增加队列的大小。可是从Linux内核版本2.6.6-mm2开始, 这个参数就不再使用了,并且对于每个进程有一个独立的信号队列,这个队列的大小可以用 RLIMIT_SIGPENDING 参数调节。当这个队列过于拥塞,nginx就放弃它并且开始使用 poll 方法来处理连接直到恢复正常。
- /dev/poll - 高效的方法,使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+.
- eventport - 高效的方法,使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装 这个 安全补丁。
[编辑] Basic HTTP features
- Handling of static files, index files, and autoindexing; open file descriptor cache;
- Accelerated reverse proxying without caching, simple load balancing and fault tolerance;
- Accelerated support without caching of remote FastCGI servers, simple load balancing and fault tolerance;
- Modular architecture. Filters include gzipping, byte ranges, chunked responses, and SSI. Multiple SSI inclusions within a single page can be processed in parallel if they are handled by FastCGI or proxied servers.
- SSL and TLS SNI support.
[编辑] Mail proxy server features
- User redirection to IMAP/POP3 backend using an external HTTP authentication server;
- User authentication using an external HTTP authentication server and connection redirection to internal SMTP backend;
- Authentication methods:
o POP3: USER/PASS, APOP, AUTH LOGIN PLAIN CRAM-MD5; o IMAP: LOGIN, AUTH LOGIN PLAIN CRAM-MD5; o SMTP: AUTH LOGIN PLAIN CRAM-MD5;
- SSL support;
- STARTTLS and STLS support.
[编辑] Other HTTP features
- name- and IP-based virtual servers;
- keep-alive and pipelined connections support;
- flexible configuration;
- reconfiguration and online upgrade without interruption of the client processing;
- access log formats, bufferred writing, and quick log rotation;
- 4xx-5xx error codes redirection;
- rewrite module;
- access control based on client IP address and HTTP Basic authentication;
- PUT, DELETE, MKCOL, COPY and MOVE methods;
- FLV streaming;
- speed limitation;
- limitation of simultaneous connections from one address.
[编辑] Nginx + Jetty
[编辑] Nginx + GlassFish
upstream glassfish { server 127.0.0.1:3000; }
[编辑] Modules
需要 ngx_http_autoindex_module 模块
目录浏览
location / { autoindex on; }
[编辑] 站点
server { listen 80; server_name blog.huihoo.com; index index.htm index.html index.php; root /var/www/blog.huihoo.com; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; client_max_body_size 30m; }
}
[编辑] 多站点
server { # php/fastcgi listen 80; server_name domain1.com www.domain1.com; access_log logs/domain1.access.log main; root html;
location ~ \.php$ { fastcgi_pass 127.0.0.1:1025; } }
server { # simple reverse-proxy listen 80; server_name domain2.com www.domain2.com; access_log logs/domain2.access.log main;
# serve static files location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/big.server.com/htdocs; expires 30d; }
[编辑] Rewrite
Discuz!在Nginx下的Rewrite
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last; rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last; rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last; rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last; rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last; break;
[编辑] 技巧
没有html后缀,也能正常显示,如:http://docs.huihoo.com/go/golang.org/doc/go1.6 且类似 Makefile .bat .bash .rc .s .go 的文件都能显示。
location / { add_header Content-Type: 'text/html; charset=utf-8';
但会导致一些浏览器无法打开 docs.huihoo.com 网页会直接下载而不会被浏览,已暂时取消 add_header 设置。
Module ngx_http_headers_module
http://book.huihoo.com/practical-postgresql/ppbook/ 的问题还没解决
支持在线播放 avi, wmv
video/x-ms-wmv wmv; video/x-msvideo avi;
[编辑] 图集
[编辑] 链接
- https://nginx.org/
- Nginx官网
- Nginx @ GitHub
- http://wiki.nginx.org
- Nginx Resources
- agentzh 的 Nginx 教程
- http://sysoev.ru/en/
- http://wiki.codemongers.com/
- Ningx代码研究
- Nginx 的中文维基
- http://download.huihoo.com/nginx/
- http://docs.huihoo.com/nginx/
- 使用 Nginx 构建一个“高”可用的 PHP 集群
- Nginx开发从入门到精通
- The Benefits of Microcaching with NGINX