一, 为什么学习 nginx
绝大多数互联网公司流量入口都选择 nginx, 这是为什么呢? 究竟 nginx 相比于 apache,tomcat 有哪些优势? 有人说 nginx 性能高; 有人说 nginx 多线程省资源; 还有人说 nginx 代码写的好? 鄙人带着探索精神想求证真相.
好久没看 C 代码了, 感觉有些生疏, Linux 源码有些多, 还是拿 nginx 这个相对的软柿子练练手.
二, Nginx 安装
nginx 官网 http://nginx.org/ , 右侧选择 download, 选择 stable version 进行安装. 建议在 Linux 系统安装, 因为公司服务器都是 Linux(默认用户已安装 gcc), 鄙人环境 Ubuntu, 来来来搞起.
- # 前置依赖
- sudo apt-get install libpcre3 libpcre3-dev
- sudo apt-get install libssl-dev
- tar -zxvf nginx-1.14.2.tar.gz
- ./configure --prefix=xxxx # 安装 nginx 目标目录
- make
- sudo make install
安装完包含 conf,html,log 和 sbin 几个目录,*_temp 目前我也不知道作用, 猜测是和缓存有关, 这个问题我们先记下来, 带着问题看代码会更有效率.
What? 源码安装太繁琐? 对 nginx 浅尝辄止可以参考如下, 鄙人是想看源码, 所以从源码安装,
sudo apt-get install nginx
三, Nginx 命令
- ./sbin/nginx -h
- nginx version: nginx/1.14.2
- Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
- Options:
- -?,-h : this help
- -v : show version and exit
- -V : show version and configure options then exit
- -t : test configuration and exit
- -T : test configuration, dump it and exit
- -q : suppress non-error messages during configuration testing
- -s signal : send signal to a master process: stop, quit, reopen, reload
- -p prefix : set prefix path (default: /home/jiaxue/Software/nginx/)
- -c filename : set configuration file (default: conf/nginx.conf)
- -g directives : set global directives out of configuration file
不翻译大家自己看, 多看一手资料有好处. 有不理解的欢迎线下交流, 一起学习, 一起进步. 微信 (453478062)
- # 启动
- sudo ./sbin/nginx
- ps -ef | grep nginx # 检查进程是否存在
不放心的用户可以用浏览器再测试下, 浏览器中输入 127.0.0.1, 看到 Welcome to nginx! 字样
纯服务器用户可以用如下方法测试:
- sudo apt-get install curl
- curl localhost
- # 返回
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <title>
- Welcome to nginx!
- </title>
- <style>
- body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial,
- sans-serif; }
- </style>
- </head>
- <body>
- <h1>
- Welcome to nginx!
- </h1>
- <p>
- If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.
- </p>
- <p>
- For online documentation and support please refer to
- <a href="http://nginx.org/">
- nginx.org
- </a>
- .
- <br/>
- Commercial support is available at
- <a href="http://nginx.com/">
- nginx.com
- </a>
- .
- </p>
- <p>
- <em>
- Thank you for using nginx.
- </em>
- </p>
- </body>
- </HTML>
- # 和 nginx HTML 目录 index.HTML 一致
- # 停止
- sudo ./sbin/nginx -s quit
- # 或者
kill -15 进程号 #-9 也可以, 但是不推荐
四, 后续安排
nginx 配置 (基本配置 + 反向代理)
介绍事件模型
学习 nginx 源码
水了一篇, 文笔粗糙, 各位见谅. 希望广交天下好友, 一起学习一起进步. 北京互联网从业多年, 当过校招社招面试官, 擅长大数据, 流计算, 风控 (反作弊), 广告系统.
来源: http://www.jianshu.com/p/d6aa3d66e872