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. maven开发工具安装

    Maven安装并测试步骤: 1.下载并解压meaven.zip: 2.配置环境变量“M2_HOME”指向meaven安装目录: 3.添加“%M2_HOME%\bin;”到path环境变量中: 4.测试 ...

  2. WPF HyperLink链接下划线隐藏

    两种方法: 1.在Grid标签内添加资源样式. <Grid.Resources> <Style TargetType="Hyperlink"> <Se ...

  3. Neumann's Principle and Curie laws

    Neumann's Principle Neumann's principle, or principle of symmetry, states that, if a crystal is inva ...

  4. node搭建简单的本地服务器

    首先要安装node,方法很多,可以去网上找找,可以直接去官网下载安装,新版本的node是自带npm的: 安装好以后,新建一个js文件,名为server.js: let http = require(' ...

  5. CTF题-http://120.24.86.145:8002/flagphp/:Bugku----flag.php

    今天做了一道关于序列化的题目,收益颇多,愉快地开始. 首先,提示了“hint”,所以尝试加入hint参数.这儿没啥好说的,最后hint=1显示了重点内容.如下图所示 没错,是金灿灿的网页代码,开心,仔 ...

  6. 100-days: Four

    Title: Weekend 'catch-up sheep' is a lie catch-up n.补做:赶做 play catch-up 通过追赶,达到同样的水平或程度 catch-up sle ...

  7. vue-webpack项目本地开发环境设置代理解决跨域问题

    前言: 一般跨域问题只要后端配置好的话,是不需要前端做处理的,但也不能保证你遇到的所有后端都能很好的处理这个问题,这个时候可能就需要前端设置代理解决这个问题了. 配置方法: 1. config/ind ...

  8. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

  9. iOS 证书申请新步骤

    2018iOS完整的证书申请和打包过程 - 简书https://www.jianshu.com/p/2b3c2693f4f2

  10. os & sys

    os os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.c ...