博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【nginx】配置详解
阅读量:4922 次
发布时间:2019-06-11

本文共 4413 字,大约阅读时间需要 14 分钟。

nginx作为web server,是最常用的网络服务器。来看下它的配置文件。

实验1:最基础的配置(可访问静态资源)

包括listen,server_name,root,index,access_log, error_log

server {    listen 80;     server_name lww.demo.com;    root /home/luwenwei/dev/project/demo.com;    index index.html;    access_log /data/nginx/logs/lww.access.log;    error_log /data/nginx/logs/lww.error.log;}

创建项目文件夹:mkdir /home/luwenwei/dev/project/demo.com

创建文件:index.html

然后启动nginx:service nginx start

访问网站:curl -H "Host: lww.demo.com" http://127.0.0.1/index.html,可以看到文件中的内容。

解释:

listen:监听端口,默认是80

server_name:项目的域名

root:项目根目录

index:项目默认访问地址,访问: http://server_name/会自动路由到index配置的文件

access_log和error_log:访问日志和错误日志

实验2:直接返回http_code和body内容

location /200 {        return 200 "I am env\n";    }       location /500 {        return 500 "I am 500 page\n";    }

再实验1的基础上加上两个配置。

测试访问:curl -H "Host: lww.demo.com" http://127.0.0.1/200 -v

输出:

*   Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /200 HTTP/1.1> Host: lww.demo.com> User-Agent: curl/7.47.0> Accept: */*> < HTTP/1.1 200 OK< Server: nginx/1.10.3 (Ubuntu)< Date: Mon, 12 Feb 2018 07:35:22 GMT< Content-Type: application/octet-stream< Content-Length: 9< Connection: keep-alive< I am env* Connection #0 to host 127.0.0.1 left intact

可以看到输出的http_code和body内容和我们设置的一致,符合预期。

访问测试:curl -H "Host: lww.demo.com" http://127.0.0.1/500 -v

*   Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /500 HTTP/1.1> Host: lww.demo.com> User-Agent: curl/7.47.0> Accept: */*> < HTTP/1.1 500 Internal Server Error< Server: nginx/1.10.3 (Ubuntu)< Date: Mon, 12 Feb 2018 07:36:25 GMT< Content-Type: application/octet-stream< Content-Length: 14< Connection: keep-alive< I am 500 page* Connection #0 to host 127.0.0.1 left intact

可以看到输出的内容,http_code=500,和我们设置的一致,符合预期。

一定要注意这里有location的语法,如果直接全部匹配,使用:location /URI即可。

 实验3:rewrite

location /304 {        rewrite /304 /200 last;    }

rewrite的语法:rewrite <regex> <replacement> <flag>

rewrite更多的语法可以参见:https://www.cnblogs.com/czlun/articles/7010604.html

flat: break, last, redirect, permanent

测试:

$ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v*   Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /304 HTTP/1.1> Host: lww.demo.com> User-Agent: curl/7.47.0> Accept: */*> < HTTP/1.1 200 OK< Server: nginx/1.10.3 (Ubuntu)< Date: Mon, 12 Feb 2018 07:43:27 GMT< Content-Type: application/octet-stream< Content-Length: 9< Connection: keep-alive< I am env* Connection #0 to host 127.0.0.1 left intact

 接下来看下,不同的<flag>下,同一个访问请求的响应结果。

$ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v* Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /304 HTTP/1.1> Host: lww.demo.com> User-Agent: curl/7.47.0> Accept: */*> < HTTP/1.1 404 Not Found< Server: nginx/1.10.3 (Ubuntu)< Date: Mon, 12 Feb 2018 07:48:08 GMT< Content-Type: text/html< Content-Length: 178< Connection: keep-alive<
404 Not Found

404 Not Found


nginx/1.10.3 (Ubuntu)
* Connection #0 to host 127.0.0.1 left intact
$ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v* Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /304 HTTP/1.1> Host: lww.demo.com> User-Agent: curl/7.47.0> Accept: */*> < HTTP/1.1 302 Moved Temporarily< Server: nginx/1.10.3 (Ubuntu)< Date: Mon, 12 Feb 2018 07:48:49 GMT< Content-Type: text/html< Content-Length: 170< Location: http://lww.demo.com/200< Connection: keep-alive<
302 Found

302 Found


nginx/1.10.3 (Ubuntu)
* Connection #0 to host 127.0.0.1 left intact
$ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v* Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> GET /304 HTTP/1.1> Host: lww.demo.com> User-Agent: curl/7.47.0> Accept: */*> < HTTP/1.1 301 Moved Permanently< Server: nginx/1.10.3 (Ubuntu)< Date: Mon, 12 Feb 2018 07:49:22 GMT< Content-Type: text/html< Content-Length: 194< Location: http://lww.demo.com/200< Connection: keep-alive<
301 Moved Permanently

301 Moved Permanently


nginx/1.10.3 (Ubuntu)
* Connection #0 to host 127.0.0.1 left intact

需要注意的是,redirect和permanent两者结果类似。

redirect:#返回302临时重定向,以及Location的header,浏览器地址会显示跳转后的URL地址

permanent:#返回301永久重定向,以及Location的header,浏览器地址栏会显示跳转后的URL地址

 

转载于:https://www.cnblogs.com/helww/p/8444694.html

你可能感兴趣的文章
javascript 学习1
查看>>
Angular应用架构设计-3:Ngrx Store
查看>>
<a>标签文件下载文件名乱码问题
查看>>
HTTP抓包
查看>>
numpy array分割-【老鱼学numpy】
查看>>
第五篇Python基本数据类型
查看>>
[WCF]WCF起航
查看>>
工作中常用的js、jquery自定义扩展函数代码片段
查看>>
JavaBean学习--练习示例
查看>>
【codeforces】【比赛题解】#915 Educational CF Round 36
查看>>
第二阶段团队冲刺10
查看>>
海量分页的简单分析
查看>>
ES6入门教程---变量和常量
查看>>
Python项目中使用配置文件
查看>>
html5的学习日志
查看>>
Python数据分析_Pandas01_数据框的创建和选取
查看>>
RESTful-rest_framework应用第一篇
查看>>
Console命令详解,让调试js代码变得更简单
查看>>
hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)
查看>>
Excel 导出
查看>>