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. centos查看命令

    1.查看 CPU 物理个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看 CPU 核心数量 grep 'core id' /proc/ ...

  2. ESXi 上创建CentOS虚拟机

    之前介绍了ESXi上添加存储.本篇介绍一下在ESXi上创建CentOS虚拟机. 方法/步骤   登陆ESXi,选择“创建/注册虚拟机” 选择“创建新的虚拟机” 给装的虚拟机命名,并选择操作系统及版本 ...

  3. Bootstarp 模版网站

    最佳Bootstrap模版 https://colorlib.com/wp/cat/bootstrap/ https://www.jianshu.com/p/4a116cf24a05

  4. 如何使用navicat远程连接服务器上的oracle数据库

  5. Dictionary,hashtable, stl:map有什么异同?

    相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...

  6. mybatis 获取insert返回的主键

    在我们开发过程中,在插入数据到数据库时,很多时候都需要把其主键返回,这里就说一下mybatis是怎么获取的. 其中mysql和oracle是不同的做法,因为mysql本身就提供字段自增的属性,而ora ...

  7. webstorm添加多个项目

    在webstorm工作目录下,添加其他项目,不用每个项目打开一个webstorm,刚开始使用webstorm的时候,每次打开一个项目的时,都要打开一个开发界面,在这几个窗口之间来回的切换,有一天真的感 ...

  8. 制作基于U盘启动和网络常识

    一.制作基于U盘启动的操作系统盘1.准备相关的软件和硬件 下载软件并安装到[电脑]中 ——大白菜.老毛桃 硬件——U盘(空的) 2.插入U盘,点击桌面上的[大白菜装机版]打开大白菜, 点击[一键制作U ...

  9. func 的参数修饰

    1.0 在声明一个 Swift的方法的时候,我们一般不去指定参数前面的修饰符,而是直接声明参数: func incrementor(variable : Int) ->Int { } 这个方法接 ...

  10. 机器学习之overfiting

    有错欢迎指正,别让小弟继续错下去. 我们在使用机器学习过程中,经常会overfiting,overfiting的产生原因是noise.训练样本大的话,还好,不用考虑这个 问题.但是,当数据量小的时候, ...