设为首页 收藏本站
查看: 1029|回复: 0

[经验分享] nginx配置文件详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-2 10:32:19 | 显示全部楼层 |阅读模式
nginx配置文件:
user nginx;
worker_processes 1; ##启动进程的个数(进程个数并不是并发越多越好。一般来说,进程个数与CPU个数相等。如果进程个数多于CPU个数,要进行上下文切换,会耗时。)
events {
  worker_connections 1024;
} ##一个进程并发请求数

nginx不仅可以做网页服务器,还可以做nginx反向代理和mail代理。
在如下行进行配置:

http {
   include mime.types;
   default_type application/cetet-stream;
   #log_format main  '$remote_addr - $remote_user [$time_local] "$request"'
   #                 '$status $body_bytes_sent "$http_referer"'
   #                 '"$http_user_agent" "$http_x_forwarded_for"';  
   #remote_addr 客户端ip remote_user远程用户 time_local本地时间 request用户请求资源 status状态码 body_bytes_sent发送的字节数 http_referer访问网页的来源链接 http_user_agent浏览器类型 http_x_forwarded_for缓存服务器软件
  access_log /var/log/nginx/access.log main;日志的存放路径,使用main格式
  sendfile on;
  keepalive_timeout 65;使用保持连接
  #gzip on;网页文件压缩以后再发送

server {
    listen       80 default_server;
    server_name  localhost;主机名称

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / { 是http://172.16.100.1/的根目录
        root   /usr/share/nginx/html;相对于网页文件存放位置的相对路径,最好使用绝对路径
        index  index.html index.htm;网页主页面
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

listen配置的格式:(只能在server中进行设置)
listen address:port[ default [ backlog=num | rcvbuf=size接收缓存大小 | sndbuf=size 发送缓存大小 | accept_filter=filter | deferred | bind | ssl 只能使用443端口]]
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;

location配置的格式:(只能在server中进行设置)
location [=|~|~*|^~|@] /url/ {...}
= 精确匹配,所请求的必须是特定的文件
~ ~* 正则表达式,url是正则表达式patter ~区分字母大小写 ~*不区分字母大小写
^~ 不使用正则表达式,只做逐个字符匹配
@ 转向其他代理服务器
优先级:= ^~ (~ ~*)

例子:
= 只能匹配/
location = / {
  #matches the query / only.
  [ configuration A ]
}

/ 可以匹配所包含的目录
location / {
  #matches any query, since all queries begins with /, but regular expresions and any longer conventional blocks will be matched first.
  [ configuration B ]
}

^~ 匹配以后面URL定义起始的目录
location ^~ /images/ {
  #matches any query beginning with /images/ and halts searching, so regular expressions will not be checked.
  [ configuration C ]
}

~* 匹配正则表达式
location ~* \.(gif|jpg|jpeg)$ {
  #matches any request ending in gif, jpg, or jpeg. However, all requests to the /images/ directory will be handled by Configuration C.
  [ configuration D ]
}

Example requests:
* / --> configuration A
* /documents/document.html --> configuration B
* /images1.gif --> configuration C
* /documents/1.jpg --> configuration D

alias 设定别名,在location中定义
语法:alias file-path|directory-path
location / {
  root /spool/w3;
}
location /bbs/ {
  alias /spool/bbs/;
}

index 设置主页面

autoindex 如果访问时候没有主页面,就列出该目录下的所有页面
location / {
  autoindex on;
}

AccessModule 访问控制列表
location / {
  deny 192.168.1.1;
  allow 192.168.1.0/24;
  allow 10.1.0.0/16;
  deny all;
}

开启nginx状态监控的功能;
location /nginx status {
  stub_status on;
  access_log off;关闭访问日志
}
Active connections:2 处于活动状态的连接
server accepts handled requests
125202 125202 125254
(已经接入的连接数 已经处理过的连接数 正在处理的连接数 )
Reading:1 Writing:1 Waiting:0
(读取请求头 处理过请求,并将结果发送给客户端 尚处于保活状态的连接数)

启动基于用户的认证:
server {
  server_name www.magedu.com;
  ...
  location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
    ...
  }
}
#htpasswd -cm /etc/nginx/.htpasswd jerry


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-21460-1-1.html 上篇帖子: nginx 配置web 虚拟目录 并且codeIgniter,thinkphp 重新url 地址 下篇帖子: linux下安装nginx与nginx调优 配置文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表