本文参考自: 原文地址
1.定义
nginx是一款http服务器,也是邮件代理服务器、tcp代理服务器等,一般用于处理静态资源,这点与apache的http服务器类似,http服务器简单来说,是绑定ip,并监听某个端口的请求,并作出相应的回应。
2.应用场景
2.1 http服务器
2.2 负载均衡
2.3 代理服务器
3.架构
3.1 主线程和工作线程
有一个主线程和N个工作线程,主线程用于加载和验证配置文件以及维护工作线程,工作线程基于事件模型等,高效的分发请求,工程线程数量可配,可配置cpu个数。
3.2 配置文件
配置文件的核心是定义如何接收和处理请求,包含两种指令:简单指令和块指令。
3.2.1 配置文件结构
简单指令是key+空格+value的形式,块指令是包含多个简单指令;块指令也可包含块指令;
3.2.2 配置文件解释
http模块 server模块 location模块
http模块是定义http请求相关的模块,比较重要;server模块是http模块的子模块,定义服务器的;location是server模块的子模块,定义url路径匹配以及请求转发路径的。http模块可包含多个server模块,server模块可含多个location模块。
3.3 模块
4.使用方法
4.1 nginx.conf配置
4.1.1 静态分离
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
4.1.2 代理服务
server {
location / {
proxy_pass http://localhost:8080/;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
4.1.3 FastCGI 代理
server {
location / {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
4.1.4 负载均衡
upstream zpserver1{
server 127.0.0.1:8080 weight=9;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://zpserver1;
}
#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;
#}
}
4.2 命令操作
参考地址:
1.https://lufficc.com/blog/configure-nginx-as-a-web-server
2.https://segmentfault.com/a/1190000007803704