Nginx
nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 25.64% busiest sites in April 2016. Here are some of the success stories: Netflix, Wordpress.com, FastMail.FM.
The sources and documentation are distributed under the 2-clause BSD-like license.
Commercial support is available from Nginx, Inc.
Nginx是一款輕量級的Web處事器/反向署理處事器及電子郵件(IMAP/POP3)署理處事器,由俄羅斯工程師Igor
Sysoev開拓,供俄國大型進口網站及搜索引擎Rambler利用.其源代碼以BSD-like協議宣布.
其特點是內存占用少,并發本領強,
因此被海內許多大型網站(如京東/淘寶)回收.
(主頁/先容/文檔).
編譯安裝
目次 | 描寫 |
---|---|
conf | nginx設置文件 |
html | 網頁文件 |
logs | nginx日志文件 |
sbin | nginx可執行文件 |
/usr/local/nginx/sbin/nginx
啟動, Nginx默認占用80端口,可在nginx.conf中變動.
Nginx信號
關于Linux信號相關常識, 可參考系列博客Linux信號實踐, 在此, 我們僅先容Nginx捕捉的幾個信號.
nginx can be controlled with signals.
信號名 | 浸染 |
---|---|
TERM/INT | fast shutdown |
QUIT | graceful shutdown |
HUP | changing configuration, keeping up with a changed time zone , starting new worker processes with a new configuration, graceful shutdown of old worker processes |
USR1 | re-opening log files |
USR2 | upgrading an executable file |
WINCH | graceful shutdown of worker processes |
以上信號浸染亦可通過編譯出的nginx二進制文件實現:
To start nginx, run the executable file. Once nginx is started,
it can be controlled by invoking the executable with the -s
parameter. Use the following syntax:
nginx -s signal
設置
"hljs-preprocessor">#運行處事的用戶及用戶組
#user nobody;
#事情歷程數(一般配置為CPU數*焦點數)
worker_processes ;
#全局錯誤日志(位置/級別)
#error_log logs/ "hljs-keyword">error.log;
#error_log logs/ "hljs-keyword">error.log notice;
#error_log logs/ "hljs-keyword">error.log info;
#PID文件
#pid logs/nginx.pid;
#事情模式及毗連數上限
events {
# 回收epoll舉辦IO復用
# use epoll;
# 單歷程所答允的最大毗連數
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
"hljs-preprocessor">#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
"hljs-preprocessor"># '$status $body_bytes_sent "$http_referer" '
"hljs-preprocessor"># '"$http_user_agent" "$http_x_forwarded_for"';
"hljs-preprocessor">#access_log logs/access.log main;
#是否激活sendfile()函數
sendfile on;
"hljs-preprocessor">#將HTTP響應頭壓縮到一個包中發送,僅在sendfile開啟時才氣利用
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout ;
#開啟gzip模塊
#gzip on;
#設定虛擬主機,默認監聽80端口,可設置多個
server {
listen ;
server_name localhost;
#charset koi8-r;
"hljs-preprocessor">#access_log logs/host.access.log main;
# 頁面存放位置
location / {
# 頁面存放根目次
root html;
# 默認頁面
index index.html index.htm;
}
"hljs-preprocessor">#error_page 404 /404.html;
"hljs-preprocessor"># redirect server "hljs-keyword">error pages to the static page /50x.html
#
error_page /x.html;
location = /x.html {
root html;
}
"hljs-preprocessor"># proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
"hljs-preprocessor"># proxy_pass http://127.0.0.1;
#}
"hljs-preprocessor"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
"hljs-preprocessor"># root html;
"hljs-preprocessor"># fastcgi_pass 127.0.0.1:9000;
"hljs-preprocessor"># fastcgi_index index.php;
"hljs-preprocessor"># fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
"hljs-preprocessor"># include fastcgi_params;
#}
"hljs-preprocessor"># deny access to .htaccess files, "hljs-keyword">if Apache's document root
"hljs-preprocessor"># concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
"hljs-preprocessor"># another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
"hljs-preprocessor"># listen somename:8080;
"hljs-preprocessor"># server_name somename alias another.alias;
# location / {
# root html;
"hljs-preprocessor"># index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
"hljs-preprocessor"># listen 443 ssl;
"hljs-preprocessor"># server_name localhost;
"hljs-preprocessor"># ssl_certificate cert.pem;
"hljs-preprocessor"># ssl_certificate_key cert.key;
"hljs-preprocessor"># ssl_session_cache shared:SSL:1m;
"hljs-preprocessor"># ssl_session_timeout 5m;
"hljs-preprocessor"># ssl_ciphers HIGH:!aNULL:!MD5;
"hljs-preprocessor"># ssl_prefer_server_ciphers on;
# location / {
# root html;
"hljs-preprocessor"># index index.html index.htm;
# }
#}
}
Server
在Nginx內可設置虛擬主機, 只需在nginx.conf中添加一個server元素: