【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; #设定本 ...
随机推荐
- 在 MySQL 中创建一个中文数据库
安装完 MySQL 后,要修改密码. step 1: SET PASSWORD = PASSWORD('your new password'); step 2: ALTER USER 'root'@' ...
- struts2前后台传值的三种方法
原文地址: http://laokaddk.blog.51cto.com/368606/1340816 多的不说,直接上代码; struts.xml代码: <?xml version=" ...
- 《深入理解java虚拟机》笔记
二.java内存区域与内存溢出异常 0.在内存管理领域,java与c/c++不同的是,在java虚拟机自动内存管理机制下,java不需要手动去为对象写配对的free内存的代码,不容易出现内存泄漏和内存 ...
- c#: TabControl隐藏选项卡(WizardPages)
如Delphi之TPageControl控件,其TTabSheet有TabVisible属性,在制作类似Wizard页面切换时,甚为有用. 而c#对应之TabControl控件,其页面TabPage无 ...
- 项目总结07:JS图片的上传预览和表单提交(FileReader()方法)
JS图片的上传预览和表单提交(FileReader()方法) 一开始没有搞明白下面这块代码的,今天有时间简单整理下 核心点:FileReader()方法 以下是代码(以JSP文件为例) <!DO ...
- Django使用jsonp和cors解决跨域请求问题
1.使用jsonp的方式解决跨域请求的问题 我启动两个django项目,然后使用的端口不一样,在项目1中通过ajax发请求给项目2,然后接受项目2发送过来的数据 先看项目1的ajax的代码 $(&qu ...
- Server2003+IIS6+TP-Link+花生壳配置
Server2003+IIS6+TP-Link+花生壳配置外网一共分四步: 固定Server2003电脑的局域网IP地址. 设置IIS网站中的TCP端口. 在TP-Link中设置转发规则. 申请花生壳 ...
- swift - 高斯模糊
/* case extraLight case light case dark @available(iOS 10.0, *) case regular @available(iOS 10.0, ...
- day 12 内置函数,装饰器,递归函数
内置函数 内置函数:python给咱们提供了一些他认为你会经常用到的函数,68种 内置函数 abs() dict() help() min() setattr() all() di ...
- LIS LCS 最长上升子序列 最长公共子序列 ...
最长上升子序列,问题定义:http://blog.csdn.net/chenwenshi/article/details/6027086 代码: public static void getData( ...