【Web】Nginx配置规则
Nginx配置基本说明
以下是nginx的基本配置文件如下(编辑命令:vi /usr/local/nginx/conf/nginx.conf):
#user nobody;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 1; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #进程pid文件
#pid logs/nginx.pid; events {
#单个进程最大连接数(最大连接数=连接数*进程数),一个请求的连接一般是2(静态)和4(代理)
worker_connections 1024;
} #设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream; #日志格式设定
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #开启高效文件传输模式
sendfile on;
#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#tcp_nopush on; #长连接超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65; #gzip模块设置
#gzip on; #虚拟主机的配置
server {
#监听端口
listen 80;
#域名可以有多个,用空格隔开
server_name localhost; #charset koi8-r; #定义本虚拟主机的访问日志
#access_log logs/host.access.log main;
#代理位置
location / {
root html; //根目录
index index.html index.htm; //首页 } #错误页
#error_page 404 /404.html;
#重定向错误页
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
检查配置文件是否正确命令:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

Location配置规则
location
语法:location [=|~|~*|^~] /uri/{...}
使用范围:server中使用
这个参数根据URI的不同需求进行配置,可以使用字符串与正则表达式匹配,如果要使用正则表达式,你必须制定下列前缀:
- ~:区分大小写
- ~*:不区分大小写
- ^*:禁止表达式匹配
- =:精确匹配
例子如下:
location = / {
#只匹配/的查询
[configuration A]
}
location / {
#匹配任何以/开始的查询,但是正则表达式与一些长的字符串将被首先匹配
[configuration B]
}
location ^- /images/ {
#匹配任何以/images/开始的查询并且停止搜索,不检查正则表达式
[configuration C]
}
location ~* \.(gif|jpg|png)$ {
#匹配任何以gif|jpg|png结尾的文件,但是所有/images/目录的请求在configuration C处理
[configuration D]
}
各请求的计算如下:
./ -> configuration A
./documents/document.html -> configuration B
./images/1.gif -> configuration C
./documents/1.jpg -> configuration D
其他功能配置
配置访问日志及错误日历
- 去掉配置文件前端的日志文件格式注释

- 在server模块中,设置访问日志地址,以及错误日志地址

- 重启nginx,并访问server即可看到相应的日志文件中有日志。
配置错误页面
- 在server模块中,设置error_page,并且可以指定错误页面的根目录

- 重启nginx,并访问server报错,即可看到制定的错误界面。
配置自动索引及别名功能
- 在server模块中,加入一个location,开启自动索引,然后在更目录下新建文件data和其他文件,如下:

在浏览器中输入http://server/data,进行访问,如下:
- 在server模块中,进入一个location,开启别名功能,如下:

在浏览器中输入http://server/b,进行访问,请求实际访问的地址是/usr/local/nginx/html2。
配置文件浏览器缓存
- 设置图片缓存时间为1天
配置下载限速
- nginx可对下载文件进行限制,在location中加入参数:

配置访问控制及身份验证
- 在location中加入参数如下:

- 配置密码文件,在/usr/local/nginx目录下新建文件.htpasswd文件,并且使用命令添加用户名和密码,命令:printf "test:$(openssl passwd -crypt 123456)\n" >>/usr/local/nginx/.htpasswd

其中用户名是:test,密码是:123456 - 在浏览器中进行访问,需要密码进行登陆。如下

配置htts代理
- 需要nginx支持ssl模块
即编译nginx加入此模块(--with-http_ssl_module),命令:./configure --prefix=/data/soft/nginx --with-http_ssl_module - 配置文件配置如下
location /https {
proxy_http_version 1.1;
proxy_pass https://www.baidu.com;
proxy_set_header X-B3-TraceId $request_id;
proxy_set_header X-Span-Name $uri;
proxy_set_header Host www.baidu.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} - 重启nginx,即可代理https的请求
【Web】Nginx配置规则的更多相关文章
- 2-4、nginx特性及基础概念-nginx web服务配置详解
Nginx Nginx:engine X 调用了libevent:高性能的网络库 epoll():基于事件驱动event的网络库文件 Nginx的特性: 模块化设计.较好扩展性(不支持模块动态装卸载, ...
- Centos下Nginx配置WEB访问日志并结合shell脚本定时切割
在一个成熟的WEB系统里,没有日志管理是不可以的,有了日志,可以帮助你得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某个服务或server的性能瓶颈等. ...
- nginx 静态目录配置规则,路径匹配与本地资源
经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使 用以下规则 假如nginx是在本机,静态目录也是在本机, 1.子目录匹配 如下配置 location ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...
- codeigniter nginx rewrite规则配置【转】
转自:http://www.nginx.cn/1134.html nginx如何配置才能支持codeigniter ? 1. codeigniter的url美化去掉index.php 1 2 3 ...
- 你真的了解如何将 Nginx 配置为Web服务器吗
阅读之前,建议先阅读初识 Nginx. 之后,我们来了解一下 Nginx 配置. 抽象来说,将 Nginx 配置为 Web 服务器就是定义处理哪些 URLS 和如何处理这些URLS 对应的请求.具体来 ...
- Nginx配置location及rewrite规则
Nginx配置location及rewrite规则 示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } loca ...
- nginx配置location总结及rewrite规则写法【转】
转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...
- nginx 配置web 虚拟文件夹 而且codeIgniter,thinkphp 重定向url 地址
nginx 配置虚拟文件夹而且url 重定向 server { #侦听80port listen 8090; #定义使用www.xx.com訪问 server_name 127.0.0.1; #设定本 ...
随机推荐
- python写机器人玩僵尸骰子
python写机器人玩僵尸骰子由Al Sweigart用python发布注意:我正在为我的僵尸骰子模拟器寻找反馈,以及这一套指令.如果你觉得有什么地方可以改进,请发邮件到al@inventwithpy ...
- 一个范围的两个数进行数位的累加,实现对两个数num1和num2的数位和相加
对一个范围的两个数进行数位的累加,例如有两个数 15,19则 他们的数位和应该为: 1+5+1+6+1+7+1+8+1+9,结果为40. 测试说明 样例1 输入:1519 输出: 40 以下是不同方法 ...
- pta l3-7(天梯地图)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805051153825792 题意:给定n个地点,m条边以及边的 ...
- TCP粘包、拆包
TCP粘包.拆包 熟悉tcp编程的可能都知道,无论是服务端还是客户端,当我们读取或发送数据的时候,都需要考虑TCP底层的粘包/拆包机制. TCP是一个“流”协议,所谓流就是没有界限的遗传数据.可以想象 ...
- [Java学习]常用类-包装类型
八种基本类型对应的包装类 Java中的数据类型由八种基本类型,以及引用类型组成. byte short int long float double boolbean char Object 为了方便, ...
- ajax POST跨域请求完美解决
方式: js前端请求: function getOcrInfo(imageData){$.ajax({ url: 'http://localhost:8080/LSWS/ws/ocr/getWeb ...
- Django xadmin后台添加富文本编辑器UEditor的用法
效果图: 步骤: 1.利用命令:pip install DjangoUeditor,安装DjangoUeditor,但由于DjangoUeditor没有python3版本的,从的Github上把修改好 ...
- Java项目学习笔记(一)
2017/2/27 一.target属性 <a>标签的target属性规定在什么地方打开该链接文档. 1.打开新窗口,将文档重定向到一个单独的窗口. <a href="a. ...
- Mysql数据库(一)
一 什么是数据库 一般来说,所有的数据都要存储在硬盘中,为了方便对这些数据的管理因此就出现了例如MySQL SQLserver oracle等数据库管理软件. 数据库中的数据按一定的数据模型组织.描述 ...
- pthreads v3下的worker和pool的使用
有些人会想,明明用thread已经可以很好的工作了,为什么还要搞个worker和pool? 之所以要用到worker和pool还是因为效率,因为系统创建一个新线程代价是比较昂贵,每个创建的线程会复制当 ...