Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nginx基础知识

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 Nginx基础知识

Nginx基础知识

Avatar for Allen Liu

Allen Liu

June 24, 2012
Tweet

More Decks by Allen Liu

Other Decks in Technology

Transcript

  1. 8 安装指南 安装指南 安装指南 安装指南 • 安装命令:apt-get install nginx •

    重要路径: 1,运行文件路径:/usr/sbin/nginx 2,配置文件路径:/etc/nginx/
  2. 15 配置文件 配置文件 配置文件 配置文件- - - -语法规则 语法规则 语法规则

    语法规则- - - -全局配置 全局配置 全局配置 全局配置 • user www www • worker_proccesses 4 • worker_rlimit_nofile 10240 • pid /var/run/nginx.pid • error_log /var/error.log info [ debug | info | notice | warn | error | crit]
  3. 16 配置文件 配置文件 配置文件 配置文件- - - -语法规则 语法规则 语法规则

    语法规则-server -server -server -server • listen 80 • server_name localhost • access_log /var/log/access.log main [main,timing,up_head,mine] • log_format main '$remote_addr - $remote_user [$time_local] '
  4. 17 配置文件 配置文件 配置文件 配置文件- - - -负载均衡 负载均衡 负载均衡

    负载均衡- - - -样例 样例 样例 样例 http { upstream myproject { server 127.0.0.1:8000 weight=3; server 127.0.0.1:8001 } server { listen 80; server_name www.domain.com; location / { proxy_pass http://myproject; } } }
  5. 18 配置文件 配置文件 配置文件 配置文件- - - -负载均衡 负载均衡 负载均衡

    负载均衡- - - -策略 策略 策略 策略 策略: 策略: 策略: 策略:轮询,weight,ip_hash ip_hash 写法: upstream myproject { server 127.0.0.1:8000; server 127.0.0.1:8001; ip_hash; } 补充: 补充: 补充: 补充:down,backup
  6. 19 配置文件 配置文件 配置文件 配置文件- - - -整合 整合 整合

    整合PHP-nginx PHP-nginx PHP-nginx PHP-nginx相关配置 相关配置 相关配置 相关配置 server { listen 80; server_name www.domain.com; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } }
  7. 20 配置文件 配置文件 配置文件 配置文件- - - -整合 整合 整合

    整合PHP-php-fpm PHP-php-fpm PHP-php-fpm PHP-php-fpm • 必要编译参数: 必要编译参数: 必要编译参数: 必要编译参数:--enable-fpm • 必要配置选项: 必要配置选项: 必要配置选项: 必要配置选项:listen 127.0.0.1:9000 • 管理命令: 管理命令: 管理命令: 管理命令: 1,php-fpm 2,kill -INT `cat /var/php-fpm.pid` 3,kill -USR2 `cat /var/php-fpm.pid`
  8. 21 配置文件 配置文件 配置文件 配置文件-rewrite- -rewrite- -rewrite- -rewrite-指令列表 指令列表 指令列表

    指令列表 • set set set set • break break break break • return return return return • if if if if • rewrite rewrite rewrite rewrite
  9. 22 配置文件 配置文件 配置文件 配置文件-rewrite- -rewrite- -rewrite- -rewrite-指令: 指令: 指令:

    指令:if if if if • 相等与不等: 相等与不等: 相等与不等: 相等与不等:= = = =, , , ,!= != != != • 检测符号: 检测符号: 检测符号: 检测符号:-f,-d,-e,-x -f,-d,-e,-x -f,-d,-e,-x -f,-d,-e,-x • 匹配正则: 匹配正则: 匹配正则: 匹配正则: ~,大小写敏感匹配 ~,大小写敏感匹配 ~,大小写敏感匹配 ~,大小写敏感匹配 ~ ~ ~ ~* * * *,大小写不敏感匹配 ,大小写不敏感匹配 ,大小写不敏感匹配 ,大小写不敏感匹配 ! ! ! !~,大小写敏感不匹配 ~,大小写敏感不匹配 ~,大小写敏感不匹配 ~,大小写敏感不匹配 ! ! ! !~ ~ ~ ~* * * *,大小写不敏感不匹配 ,大小写不敏感不匹配 ,大小写不敏感不匹配 ,大小写不敏感不匹配
  10. 23 配置文件 配置文件 配置文件 配置文件-rewrite- -rewrite- -rewrite- -rewrite-指令: 指令: 指令:

    指令:if if if if例子 例子 例子 例子 例子 例子 例子 例子1 1 1 1: : : : if ($request_method = POST ) { if ($request_method = POST ) { if ($request_method = POST ) { if ($request_method = POST ) { return 405; return 405; return 405; return 405; } } } } 例子 例子 例子 例子2 2 2 2: : : : if (!-f $request_filename) { if (!-f $request_filename) { if (!-f $request_filename) { if (!-f $request_filename) { break; break; break; break; proxy_pass http://127.0.0.1; proxy_pass http://127.0.0.1; proxy_pass http://127.0.0.1; proxy_pass http://127.0.0.1; } } } } 例子 例子 例子 例子3 3 3 3: : : : if ($http_user_agent ~ MSIE) { if ($http_user_agent ~ MSIE) { if ($http_user_agent ~ MSIE) { if ($http_user_agent ~ MSIE) { rewrite ^(. rewrite ^(. rewrite ^(. rewrite ^(.* * * *)$ /msie/$1 )$ /msie/$1 )$ /msie/$1 )$ /msie/$1 break; break; break; break; } } } }
  11. 24 配置文件 配置文件 配置文件 配置文件-rewrite- -rewrite- -rewrite- -rewrite-指令: 指令: 指令:

    指令:rewrite rewrite rewrite rewrite • 用法: 用法: 用法: 用法:rewrite rewrite rewrite rewrite 正则 正则 正则 正则 替换 替换 替换 替换 标志位 标志位 标志位 标志位 • rewrite rewrite rewrite rewrite的生效区块为 的生效区块为 的生效区块为 的生效区块为server, location, if server, location, if server, location, if server, location, if • rewrite rewrite rewrite rewrite只对相对路径进行匹配 只对相对路径进行匹配 只对相对路径进行匹配 只对相对路径进行匹配, , , ,不处理 不处理 不处理 不处理host host host host。 。 。 。 • 标志位: 标志位: 标志位: 标志位: 1. break:停止rewrite检测,rewrite的最终结果。 • last:停止当前区块rewrite检测,转而去其他location尝 试匹配。 • redirect:返回302,临时重定向。 1. permanent:返回301,永久重定向。
  12. 25 配置文件 配置文件 配置文件 配置文件-rewrite- -rewrite- -rewrite- -rewrite-指令: 指令: 指令:

    指令:rewrite rewrite rewrite rewrite例子 例子 例子 例子 break例子: rewrite ^/media/(.*)\..*$ $1/mp3 break; last例子: rewrite ^/media/(.*)\..*$ $1/mp3 last; redirect例子: rewrite ^(.*)$ http://newdomain.com/ redirect; permanent例子: rewrite ^(.*)$ http://newdomain.com/ permanent;