资源1: 官网:
资源2: , , 资源3: 资源4: ,当前Stable版本是nginx-1.14.0,资源5: 资源6: , ,安装资源: nginx源码地址(版本1.11.2):
安装资源: echo模块安装地址(版本):安装参考:安装时间:2018-09-12
一:从源码安装nginx和echo-nginx-module模块(推荐)
01: 准备nginx和echo-nginx-module模块源码
nginx版本为1.11.2,echo-nginx-module版本为0.61
vagrant@qianjin:~$ wget http://nginx.org/download/nginx-1.11.2.tar.gzvagrant@qianjin:~$ wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gzvagrant@qianjin:~$ ls //查看一下,压缩包在当前目录下code Dockerfile nginx-1.11.2.tar.gz v0.61.tar.gzvagrant@qianjin:~$ tar -zxvf nginx-1.11.2.tar.gzvagrant@qianjin:~$ tar -zxvf v0.61.tar.gz
02: 安装
vagrant@qianjin:~$ cd nginx-1.11.2 //到解压后的nginx目录中vagrant@qianjin:~/nginx-1.11.2$ ./configure --prefix=/opt/nginx \ --add-module=/home/vagrant/echo-nginx-module-0.61// –prefix为nginx安装位置,–add-module为需要添加的模块路径,这里添加解压后echo-nginx-module路径// 执行配置后,目录下多了 Makefile文件和 objs目录vagrant@qianjin:~/nginx-1.11.2$ make -j2//报错:src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=] h ^= data[2] << 16; ~~^~~~~~~~~~~~~~~~src/core/ngx_murmurhash.c:38:5: note: here case 2: ^~~~src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=] h ^= data[1] << 8; ~~^~~~~~~~~~~~~~~src/core/ngx_murmurhash.c:40:5: note: here case 1: ^~~~原因,是将警告当成了错误处理,打开/home/vagrant/nginx-1.11.2/objs/Makefile,CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g (去年-Werror)再重新make -j2-Wall 表示打开gcc的所有警告-Werror,它要求gcc将所有的警告当成错误进行处理vagrant@qianjin:~/nginx-1.11.2$ make -j2vagrant@qianjin:~/nginx-1.11.2$ make install
03: 测试
vagrant@qianjin:~$ sudo /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf// -c 指定配置文件的路径sudo /opt/nginx/sbin/nginx -h了解更多用法在浏览器中输入 http://192.168.10.10/,如果出现Welcome to nginx!页面表示安装成功
04: 配置
建立链接
sudo ln -s /opt/nginx/sbin/nginx /usr/bin/nginx // 建立软链接,/usr/local/bin包含于$PATH当中,不需要额外的设置环境变量了,可以在任何目录运行nginx命令// 现在可以sudo nginx启动,用sudo nginx -s stop等
开机启动
编译安装需要自己进行设置方可自动启动
# 设置nginx自启动,在/lib/systemd/system/ 目录下创建一个服务文件vim /lib/systemd/system/nginx.service内容如下:[Unit]Description=nginx - high performance web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop[Install]WantedBy=multi-user.target文件说明[Unit]部分Description:描述服务After:依赖,当依赖的服务启动之后再启动自定义的服务[Service]部分Type=forking是后台运行的形式ExecStart为服务的具体运行命令(需要根据路径适配)ExecReload为重启命令(需要根据路径适配)ExecStop为停止命令(需要根据路径适配)PrivateTmp=True表示给服务分配独立的临时空间注意:启动、重启、停止命令全部要求使用绝对路径[Install]部分服务安装的相关设置,可设置为多用户# 设置了自启动后,任意目录下执行systemctl enable nginx.service# 启动nginx服务systemctl start nginx.service# 设置开机自动启动systemctl enable nginx.service# 停止开机自动启动systemctl disable nginx.service# 查看状态systemctl status nginx.service# 重启服务systemctl restart nginx.service# 查看所有服务systemctl list-units --type=service// 我还是比较喜欢用下面的方式vagrant@qianjin:~$ sudo service nginx stopvagrant@qianjin:~$ sudo service nginx star// 实际下面敲的代码少vagrant@qianjin:~$ sudo nginx #启动vagrant@qianjin:~$ sudo nginx -s stop 停止
最简单的nginx配置文件(只支持静态html)
events{}http { server { server_name symfony.cc; location / { root /var/www/study/symfony; index index.html index.htm; } }}
最简单的nginx配置文件(支持静态html和php)
前提:在hosts中定义了 168.192.10.10 symfony.cc
events{}http { server { server_name symfony.cc; root /var/www/study/symfony; location / { index index.php index.html index.htm; } location ~ \.php$ { # 在/etc/php/7.0/fpm/pool.d/www.conf的listen值与fastcgi_pass的值要相对应 # ;listen = /run/php/php7.0-fpm.sock # listen = 127.0.0.1:9000 fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }}//适合用来检查nginx是否正常,当配置出问题是,就用此配置,再在这个基础上修改,一定要明白的是,nginx只是个web服务器,碰到php文件,它也不解析,它交给php-fpm来处理,php-fpm处理完的数据再交给nginx
了解工作进程worker_process
配置位置在:在最外面
语法:worker_processes 4;
值:默认为1,一般设为cpu数量x2说明:当设为4时,1个主进程master process,4个工作进程worker process,master process的pid会动态记录在/opt/nginx/logs/nginx.pid文件中,其余4个工作进程的pid值是递加1,如主进程的pid为3082,那么别外几个工作进程的pid分别为3083,3084,3085,3086,主进程负责监控端口,协调工作进程的工作状态,分配工作任务;工作进程负责进行任务处理 vagrant@qianjin:~$ ps -ef|grep nginxroot 3082 1 0 03:56 ? 00:00:00 nginx: master process nginx -c /opt/nginx/conf/2018091201.confnobody 3083 3082 0 03:56 ? 00:00:00 nginx: worker processnobody 3084 3082 0 03:56 ? 00:00:00 nginx: worker processnobody 3085 3082 0 03:56 ? 00:00:00 nginx: worker processnobody 3086 3082 0 03:56 ? 00:00:00 nginx: worker processvagrant 3089 2311 0 03:57 pts/0 00:00:00 grep --color=auto nginx
二:测试echo模块(重点)
作用范围:location块和location if说句中
例子events{} http { server { server_name localhost; root /var/www/study/symfony; location / { index index.php index.html index.htm; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; echo "hello,word"; echo Changsha; echo $document_root; echo_flush; } }}
浏览器中输入, 网页中原来的内容不显示了,只
hello,wordChangsha/var/www/study/symfony
使用单独的location来放echo语句,不影响网页
events{}http { server { server_name localhost; root /var/www/study/symfony; location / { index index.php index.html index.htm; } location /wang { echo "hello,word"; echo -n Changsha; # -n表示这个不换行,它紧接在echo后面,其它地方忽略 echo $document_root; echo_flush; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }}
输入
hello,wordChangsha/var/www/study/symfony
例3:
location /chen { echo_duplicate 1 $echo_client_request_headers; echo "\r"; echo_read_request_body; echo $request_body; }
输出
GET /chen HTTP/1.1Host: 192.168.10.10Connection: keep-aliveCache-Control: max-age=0Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9
备注1: tar参数说明(熟悉的略过)
-c: 建立压缩档案 -x:解压(解压时要用到这个) -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这5个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。 下面的参数是根据需要在压缩或解压档案时可选的。 -z:有gzip属性的 -j:有bz2属性的 -Z:有compress属性的 -v:显示所有过程 -O:将文件解开到标准输出 下面的参数-f是必须的 -f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。
备注2:了解configure过程
checking for OS + Linux 4.15.0-32-generic x86_64checking for C compiler ... found + using GNU C compiler + gcc version: 7.3.0 (Ubuntu 7.3.0-16ubuntu3) checking for gcc -pipe switch ... found省略checking for getaddrinfo() ... foundconfiguring additional modulesadding module in /home/vagrant/echo-nginx-module-0.61 //这里添加了 echo模块 + ngx_http_echo_module was configuredchecking for PCRE library ... foundchecking for PCRE JIT support ... foundchecking for zlib library ... foundcreating objs/Makefile //生成文件MakefileConfiguration summary + using system PCRE library 使用系统PCRE库 + OpenSSL library is not used 没有用到OpenSSL库 + using system zlib library 使用系统zlib库 // 这些路径是要了解的 nginx path prefix: "/opt/nginx" nginx binary file: "/opt/nginx/sbin/nginx" nginx modules path: "/opt/nginx/modules" nginx configuration prefix: "/opt/nginx/conf" nginx configuration file: "/opt/nginx/conf/nginx.conf" 这是配置文件 nginx pid file: "/opt/nginx/logs/nginx.pid" nginx error log file: "/opt/nginx/logs/error.log" nginx http access log file: "/opt/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"