nginx配置文件
来源:互联网 发布:中信银行网络在线客服 编辑:程序博客网 时间:2024/06/07 13:23
nginx简介
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。———百度百科
nginx配置文件总览
Nginx中文文档:http://www.nginx.cn/doc/index.html
Nginx官方文档:http://nginx.org/en/docs/
nginx的配置文件的位置:
/etc/nginx/nginx.conf,使用sudo vim /etc/nginx/nginx.conf查看默认配置信息。
nginx的配置文件的主体结构:
#运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes auto; pid /run/nginx.pid; events {#事件控制,控制Nginx处理连接方式 #epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高nginx的性能 use epoll; #单个后台worker process进程的最大并发链接数 worker_connections 1024; # 并发总数是 worker_processes 和 worker_connections 的乘积 # 即 max_clients = worker_processes * worker_connections # worker_connections 值的设置跟物理内存大小有关 # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数 # 查看主机可以打开的文件句柄数是多少的命令:$ cat /proc/sys/fs/file-max } http {#HTTP配置 #设定日志格式 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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #配置上游服务器地址名为amigo_local_ends upstream amigo_local_ends { #负载均衡算法 ip_hash; server 127.0.0.1:9900; server 127.0.0.1:9901; server 127.0.0.1:9902; server 127.0.0.1:9903; } server {#虚拟主机配置 #侦听80端口 listen 80; #定义使用 storelocal.amigo.com访问 server_name storelocal.amigo.com; client_max_body_size 8m; #定义服务器的默认网站根目录位置 root /home/ych/Documents/amigo/assets; #设定本虚拟主机的访问日志 error_log /var/log/nginx/amigo.local.error_log info; #自定义不同路由使用不同的配置信息处理请求,其中调用了tornado本地配置信息 location / { try_files $uri @tornado; } #定义一个名为tornado的本地配置信息 location @tornado { proxy_pass_header Server; proxy_set_header Host $http_host;#设置请求头信息 proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://amigo_local_ends;#请求传递到上游服务器 } } #从外部加载符合当前语法结构的配置信息文件 include mime.types; }
location的路由匹配规则:
语法
location [=|~|~*|^~] /uri/ { … }
- = 开头表示精确匹配
- ^~ 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
- ~ 开头表示区分大小写的正则匹配
- ~* 开头表示不区分大小写的正则匹配
- / 通用匹配,任何请求都会匹配到
多个 location 配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =其次匹配 ^~其次是按文件中顺序的正则匹配最后是交给 / 通用匹配当有匹配成功时候,停止匹配,按当前匹配规则处理请求
官网例子:
location = / { [ configuration A ]}location / { [ configuration B ]}location /documents/ { [ configuration C ]}location ^~ /images/ { [ configuration D ]}location ~* \.(gif|jpg|jpeg)$ { [ configuration E ]}
访问路由产生的效果如下:
- 访问根目录 /, 比如 http://localhost/ 将匹配规则 A.
- 访问 http://localhost/login 将匹配规则 B,
- 访问 http://localhost/documents/a.html 将匹配规则 C
- 访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则 E ,而 http://localhost/static/c.png 则优先匹配到规则 D
后台应用服务器负载均衡配置方法
在项目中生成单独的配置文件,然后通过include加载到nginx主配置文件中。
upstream amigo_local_ends { #负载均衡算法 ip_hash; server 127.0.0.1:9900; server 127.0.0.1:9901; server 127.0.0.1:9902; server 127.0.0.1:9903; }server {#虚拟主机配置 #侦听80端口 listen 80; #定义使用 storelocal.amigo.com访问 server_name storelocal.amigo.com; client_max_body_size 8m; #定义服务器的默认网站根目录位置 root /home/ych/Documents/amigo/assets; #设定本虚拟主机的访问日志 error_log /var/log/nginx/amigo.local.error_log info; #自定义不同路由使用不同的配置信息处理请求,其中调用了tornado本地配置信息 location / { try_files $uri @tornado; } #定义一个名为tornado的本地配置信息 location @tornado { proxy_pass_header Server; proxy_set_header Host $http_host;#设置请求头信息 proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://amigo_local_ends;#请求传递到上游服务器 } }
Web站点服务器配置方法
在项目中生成单独的配置文件,然后通过include加载到nginx主配置文件中。
server { listen 8080 default_server; listen [::]:8080 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; root /home/junhua/Downloads/doc; # 首页配置 index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }
参考网址:http://www.nginx.cn/76.html
参考书籍:《Python Web 开发实战》
阅读全文
0 0
- nginx配置文件
- nginx配置文件
- Nginx配置文件
- nginx 配置文件
- nginx配置文件
- nginx配置文件
- nginx配置文件
- nginx 配置文件
- nginx配置文件
- Nginx 配置文件
- nginx配置文件
- nginx配置文件
- Nginx 配置文件
- nginx配置文件
- Nginx配置文件
- nginx 配置文件
- nginx 配置文件
- nginx配置文件
- 课堂练习1
- 把本地项目上传到Github仓库
- Genymotion下载虚拟机版本时会很慢
- JVM--GC垃圾回收器
- 框架整合----springMVC
- nginx配置文件
- shell 脚本
- android 自定义View五角星
- Yii2.0的GridView使用和原理浅析
- 密码箱 BZOJ
- RSA公钥和私钥的区别
- Batch Norm学习笔记
- leetcode 77. Combinations
- Object类