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

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

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

server {
listen ;
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 "I am env\n";
} location / {
return "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 (#)
> GET / HTTP/1.1
> Host: lww.demo.com
> User-Agent: curl/7.47.
> 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 (#)
> GET / HTTP/1.1
> Host: lww.demo.com
> User-Agent: curl/7.47.
> 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 / {
rewrite / / 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 (#)
> GET / HTTP/1.1
> Host: lww.demo.com
> User-Agent: curl/7.47.
> 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>下,同一个访问请求的响应结果。

<flag=break>
$ 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 (#)
> GET / HTTP/1.1
> Host: lww.demo.com
> User-Agent: curl/7.47.
> 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
<
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact <flag=redirect>
$ 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 Moved Temporarily
< Server: nginx/1.10. (Ubuntu)
< Date: Mon, Feb :: GMT
< Content-Type: text/html
< Content-Length:
< Location: http://lww.demo.com/200
< Connection: keep-alive
<
<html>
<head><title> Found</title></head>
<body bgcolor="white">
<center><h1> Found</h1></center>
<hr><center>nginx/1.10. (Ubuntu)</center>
</body>
</html>
* Connection # to host 127.0.0.1 left intact <flag=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 (#)
> GET / HTTP/1.1
> Host: lww.demo.com
> User-Agent: curl/7.47.
> 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
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact

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

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

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

【nginx】配置详解的更多相关文章

  1. Nginx高性能服务器安装、配置、运维 (3) —— Nginx配置详解

    四.Nginx 配置详解 YUM方式安装的Nginx默认配置文件放在/etc/nginx目录下,使用Vim编辑/etc/nginx/nginx.conf: ---------------------- ...

  2. Nginx配置详解 http://www.cnblogs.com/knowledgesea/p/5175711.html

    Nginx配置详解 序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作 ...

  3. nginx配置详解(转)

    Nginx 配置文件详解 user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error. ...

  4. Nginx配置详解(转)

    转自:Nginx简介及配置文件详解 一 Nginx简介 Nginx是一款开源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务 1.Nginx工作原理 Nginx ...

  5. 前端搭建Linux云服务器,Nginx配置详解及部署自己项目到服务器上

    目录 搭建Linux云服务器 购买与基本配置 链接linux服务器 目录结构 基本命令 软件安装 Linux 系统启动 启动过程 运行级别 Nginx详解 1.安装 方式一:yum安装 方式二:自定义 ...

  6. nginx 配置详解(转)

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  7. Nginx配置详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  8. 【转】Nginx配置详解

    转自:http://www.cnblogs.com/knowledgesea/p/5175711.html Nginx常用功能 1. Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反 ...

  9. 转发大神nginx配置详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  10. [转]Nginx配置详解

    Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也可作为 ...

随机推荐

  1. 机器学习进阶-图像金字塔与轮廓检测-轮廓检测 1.cv2.cvtColor(图像颜色转换) 2.cv2.findContours(找出图像的轮廓) 3.cv2.drawContours(画出图像轮廓) 4.cv2.contourArea(轮廓面积) 5.cv2.arcLength(轮廓周长) 6.cv2.aprroxPloyDP(获得轮廓近似) 7.cv2.boudingrect(外接圆)..

    1. cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY) # 将彩色图转换为灰度图 参数说明: img表示输入的图片, cv2.COLOR_BGR2GRAY表示颜色的变换形式 ...

  2. LeetCode 题解:Populating Next Right Pointers in Each Node I & II 二有难度。考虑不全面。

    每次应该把root同层的右侧节点传过来.如果没有,就传NULL. 同时,应该是先右后左. 感觉这次的代码还挺简洁的.. void construct(struct TreeLinkNode *root ...

  3. SQL优化 - 避免使用 IN 和 NOT IN

    WHY? IN 和 NOT IN 是比较常用的关键字,为什么要尽量避免呢? 1.效率低 项目中遇到这么个情况: t1表 和 t2表  都是150w条数据,600M的样子,都不算大. 但是这样一句查询  ...

  4. 尚硅谷springboot学习17-SpringBoot日志

    SpringBoot使用它来做日志功能: <dependency> <groupId>org.springframework.boot</groupId> < ...

  5. win10家庭版升级专业版的两种方法和密钥

    win10家庭版升级专业版密钥:VK7JG-NPHTM-C97JM-9MPGT-3V66T4N7JM-CV98F-WY9XX-9D8CF-369TT FMPND-XFTD4-67FJC-HDR8C-3 ...

  6. C++“隐藏实现,开放接口”的实现方案

    为什么要有接口? 接口就是一个程序与其它程序交流的窗口.就比如有一个电视机,我并不需要知道它是怎样工作的,我只要知道按电源键就可以开启电视,按节目加(+)减(-)可以切换电视频道就可以了. Java程 ...

  7. IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化:

    IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化: IT蓝豹 ------------------> sqlite数据库版本升级 1.sqlite升级步骤: 1.自己写一个类继承自 ...

  8. H5做的商城客户端,效果很不错

    H5做的商城客户端,效果很不错 H5做的商城客户端,效果和android原生客户端没多大区别,现在h5是越来越火了, android的小伙伴们又遇到一个新的挑战了.本项目只能学习一下WebViewAc ...

  9. 有关于tomcat启动时,利用listener来执行某个方法

    今天,项目经理让我调查一下(目的是锻炼我),刚开始的时候说用listener来实现服务器启动然后某个项目跟着启动.其实就是tomcat启动的时候去执行某个方法,通过这个方法启动某个项目.我网上调查了一 ...

  10. Ubuntu中更改所有子文件和子目录所有者权限

    转自:http://www.linuxidc.com/Linux/2015-03/114695.htm Ubuntu中有两个修改命令可以用到,「change mode」&「change own ...