【nginx】配置详解
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】配置详解的更多相关文章
- Nginx高性能服务器安装、配置、运维 (3) —— Nginx配置详解
四.Nginx 配置详解 YUM方式安装的Nginx默认配置文件放在/etc/nginx目录下,使用Vim编辑/etc/nginx/nginx.conf: ---------------------- ...
- Nginx配置详解 http://www.cnblogs.com/knowledgesea/p/5175711.html
Nginx配置详解 序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作 ...
- nginx配置详解(转)
Nginx 配置文件详解 user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error. ...
- Nginx配置详解(转)
转自:Nginx简介及配置文件详解 一 Nginx简介 Nginx是一款开源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务 1.Nginx工作原理 Nginx ...
- 前端搭建Linux云服务器,Nginx配置详解及部署自己项目到服务器上
目录 搭建Linux云服务器 购买与基本配置 链接linux服务器 目录结构 基本命令 软件安装 Linux 系统启动 启动过程 运行级别 Nginx详解 1.安装 方式一:yum安装 方式二:自定义 ...
- nginx 配置详解(转)
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- Nginx配置详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...
- 【转】Nginx配置详解
转自:http://www.cnblogs.com/knowledgesea/p/5175711.html Nginx常用功能 1. Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反 ...
- 转发大神nginx配置详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...
- [转]Nginx配置详解
Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也可作为 ...
随机推荐
- 【JEECG技术文档】表单配置-树形表单
表单配置支持树型表单了,具体效果如下图: 配置说明: 1.是否树:选择是. 2.树形表单父Id:表的自关联外键. 3.树形表单列表:显示树形图标的列,如上图中为[组织机构名称]. 4.默认值:最外层数 ...
- Tomcat 配置Https
https://www.cnblogs.com/wanghaoyuhappy/p/5267702.html JDK1.8 keytool 生存证书 C:\keys\tomcat.keystore 1: ...
- Python基本数据类型以及字符串
基本数据类型 数字 int ,所有的功能,都放在int里 a1 = 123 a1 = 456 ...
- delphi 新版内存表 FDMemTable
c++builder XE 官方demo最全60多个 http://community.embarcadero.com/blogs?view=entry&id=8761 FireDAC.Com ...
- Linux命令:unzip
语法: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir] 默认行为将zip文件中的内容全部解压缩到当前目录下. ...
- VC中BSTR、Char和CString类型的转换
1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...
- 测试Array对象的sort方法的作用。将1985,1970,1999,1998,2000,1963这些年份按升序输出。
<script type="text/javascript">var years = new Array(1985,1970,1999,1998,2000,1963); ...
- Uni2D 入门 -- Atlas转载 http://blog.csdn.net/kakashi8841/article/details/17588095
转载csdnTexture Atlas 我为什么应该使用Texture Atlas? 使用Atlas是一个普遍的好做法,而且它有很多好处.当有某些需要在屏幕渲染的时候,它背后带来的是draw call ...
- MySQL 各种主流 SQLServer 迁移到 MySQL 工具对比
原地址:https://www.cnblogs.com/overblue/p/5796887.html Mss2sql:这个比较好用
- scala spark 聚类
import org.apache.spark.ml.clustering.KMeansimport org.apache.spark.ml.evaluation.ClusteringEvaluato ...