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. tar 排除文件

    tar -cvf test.tgz test/ --exclude *.txt --exclude *.jpg

  2. VSFTP 配置虚拟用户

    虚拟用户的特点是只能访问服务器为其提供的FTP服务,而不能访问系统的其它资源.所以,如果想让用户对FTP服务器站内具有写权限,但又不允许访问系统其它资源,可以使用虚拟用户来提高系统的安全性. 在VSF ...

  3. javascript的DOM操作获取元素

    一.document.getElementById()    根据Id获取元素节点 <div id="div1"> <p id="p1"> ...

  4. BlockingQueue之DelayQueue的学习使用

    DelayQueue 是一中阻塞队列,需要实现接口Delayed定义的方法.做下使用记录和心得吧, @Datapublic class DelayQueueExample implements Del ...

  5. Vue router 的使用--初级

    在说 VueRouter 之前,首先要弄明白vueRouter 是干什么的,有什么用 说出来其实很简单,就是一个模板替换的问题,当路由改变的时候,把和路由相关的模板显示出来,就是这么简单.但是,当我们 ...

  6. jquery_ajax 地址三级联动

    jquery 的三级地址联动,原理与javascript类似,只是在触发请求的时候,使用封装好的 $.get ,$post,$.ajax 方法去执行,其余的都是一样的,后台服务器请求文件是一样的,前台 ...

  7. Array.Resize(ref arry, size);

    数组原来的内容不变,后面添加新的空间. 内部操作应该是:重新分配了一块空间,然后将旧的内容拷过去

  8. linux 覆盖可执行文件的问题

    测试环境是3.10.0 内核. 有一次操作中,发现cp -f A B执行的时候,行为不一样: 当B没被打开,则正常覆盖B. 当B是被打开,但没有被执行,则能覆盖, 当B被打开,且被执行,则不能直接覆盖 ...

  9. 【358】GitHub 上面文件夹下载方法

    参考:https://www.bilibili.com/read/cv210500/ 参考:https://www.jianshu.com/p/743ecc20ffb2 软件下载:Downloads ...

  10. 将 DNSCrypt 部署到 Openwrt 路由器上+ DNSmasq 解析国内域名用本地 DNS[ZT+实践]

    原文地址: 1.https://typcn.com/legacy/blog/posts/openwrt-dnscypt.html 2.http://www.openwrt.pro/post-376.h ...