【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服务器,也可作为 ...
随机推荐
- Tornado的安装使用
https://blog.csdn.net/a312024054/article/details/52207367 tornado原理: tornado的使用 import tornado.ioloo ...
- 4.ClassLink - 一种新型的VPC 经典网络的连接方式
阿里云CLassLink文档地址:https://help.aliyun.com/document_detail/65412.html?spm=a2c4g.11186623.2.9.41a25a07F ...
- Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter
在上一篇文章中Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream介绍了缓冲流中的字节流,而这一篇着重介绍缓冲流中字符流Buffered ...
- 【386】operator 的 itemgetter、slice、and_、or_
itemgetter 用来获取数组中指定索引的元素 from operator import itemgetter itemgetter(1, 3, 5)('ABCDEFG') output: ('B ...
- 虚拟机安装centos6.6全步骤
1.首先要下载一个centos的iso镜像,我是用虚拟机VMware来安装的,用VMware最好创建一个空白硬盘. 2.创建完毕再设置里面挂载iso的centos系统文件. 3.进入到这个页面: 说明 ...
- JS实现简单的运行代码 & 侧边广告
/* JS实现简单的运行代码功能 */<!doctype html> <html> <head> <meta charset="utf-8" ...
- [原创]HTML 用div模拟select下拉框
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns=" ...
- 27.反射2.md
目录 1.反射 2.类对象获取 3.构造函数获取 4.函数获取 4.注解反射 1.反射 定义:把一个字节码文件加载到内存中,jvm对该字节码文件解析,创造一个Class对象,把字节码文件中的信息全部存 ...
- easyUi onLoadSuccess:、onChange这些事件不能嵌套使用!!!!
easyUi onLoadSuccess:.onChange这些事件不能嵌套使用!!!!
- Jumpserver 文档
http://docs.jumpserver.org/zh/docs/admin_guide.html