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配置规则的更多相关文章

  1. 2-4、nginx特性及基础概念-nginx web服务配置详解

    Nginx Nginx:engine X 调用了libevent:高性能的网络库 epoll():基于事件驱动event的网络库文件 Nginx的特性: 模块化设计.较好扩展性(不支持模块动态装卸载, ...

  2. Centos下Nginx配置WEB访问日志并结合shell脚本定时切割

    在一个成熟的WEB系统里,没有日志管理是不可以的,有了日志,可以帮助你得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某个服务或server的性能瓶颈等. ...

  3. nginx 静态目录配置规则,路径匹配与本地资源

    经常配了nginx静态目录,死活访问不了,每次访问404.查看文档后,发现nginx配置静态目录使 用以下规则 假如nginx是在本机,静态目录也是在本机, 1.子目录匹配 如下配置 location ...

  4. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  5. codeigniter nginx rewrite规则配置【转】

    转自:http://www.nginx.cn/1134.html nginx如何配置才能支持codeigniter ? 1. codeigniter的url美化去掉index.php   1 2 3 ...

  6. 你真的了解如何将 Nginx 配置为Web服务器吗

    阅读之前,建议先阅读初识 Nginx. 之后,我们来了解一下 Nginx 配置. 抽象来说,将 Nginx 配置为 Web 服务器就是定义处理哪些 URLS 和如何处理这些URLS 对应的请求.具体来 ...

  7. Nginx配置location及rewrite规则

    Nginx配置location及rewrite规则 示例: location  = / {   # 精确匹配 / ,主机名后面不能带任何字符串   [ configuration A ] } loca ...

  8. nginx配置location总结及rewrite规则写法【转】

    转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...

  9. nginx 配置web 虚拟文件夹 而且codeIgniter,thinkphp 重定向url 地址

    nginx 配置虚拟文件夹而且url 重定向 server { #侦听80port listen 8090; #定义使用www.xx.com訪问 server_name 127.0.0.1; #设定本 ...

随机推荐

  1. xshell常用的快捷键

    删除ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dlctrl + h      删除光标所在位置前的字符相当于VIM里hx或者dhctrl + k      删除光标后面所 ...

  2. java 测试开发基础知识(类加载,JVM等)

    写在开头: 面试的时候别人很可能会问你的java原理,.class load 原理, jvm机制,这些都是Java的底层知识,特整理如下: 1. 首先,编写一个java程序,大家会用ide编写一个例如 ...

  3. Camera插件推荐,解锁电影大师级视角控制

    相机在游戏中的重要性是不言而喻的,尤其是一些MMORPG或FPS等类型的游戏,相机不仅需要跟随游戏主角进行移动,可能还要随时准备切换焦点,这就要求开发者将游戏相机管理得井井有条,能顺应游戏中可能瞬息发 ...

  4. Chrome控制台格式化输出

    一 格式化输出文字 console.log('%c你好','color:green;'); console.log('%c你好%c啊','color:green;','color:blue;'); 二 ...

  5. 【疑】checkpoint防火墙双链路切换导致丢包问题

    拓扑: 外线联通.电信各200M,通过边界交换机(纯二层,用于分线),分别接到主.备防火墙. 具体配置如下: 故障现象: 由于电信光缆中断导致电信链路不可用.大量员工反映频繁出现断网现象,通过公网注册 ...

  6. swift - xcode10 - 点击事件交互BUG - (手势和button的addTarget方法)

    1. 现象button 点击闪退:没有任何原因 ,在听云检测上,显示 BUG2: 手势 没有任何作用,哪怕设置  isUserInteractionEnabled 和isEnabled 为true 都 ...

  7. 模块math和cmath

    python使用特殊命令import导入模块,再以module.function的方式使用模块 python标准库提供了一个专门用于处理复数的模块cmath,math处理数据 模块math常用的函数有 ...

  8. Mysql数据备份与还原

    一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先 ...

  9. rbac 权限分配, 基于formset实现,批量编辑

    已经完成了  批量添加的功能. 还想要一个批量修改的功能了.随之而来的第一个问题就是,  我们的formset 并不是一条记录.而是 多条记录,甚至整个表的记录.那么显而易见的问题就是,当前端页面把数 ...

  10. jquery滚动事件

    滚动到一定高度: $(window).scroll(function(){ var scrollTop = $(document).scrollTop(); && scrollTop ...