# Nginx
## 1.概述
```
nginx 一个高性能的http和反向代理web服务器,同时提供了IAMP/POP3/SMTP服务,Nginx是有俄罗斯人开发
基于http的通信方式
特点: 反向代理
反向代理:将监听的请求,转发到对应的服务器中
负载均衡:基于反向代理,根据请求的不同,利用不同的规则进行不同同的转发
核心配置文件nginx.conf
```
## 2 nginx的安装
1、nginx官方下载地址:<http://nginx.org/>
2、下载完后将压缩包解压即可
3、nginx配置文件为根目录下conf\nginx.conf
## 3 Nginx常用命令
```
tasklist /fi "imagename eq nginx.exe" 查看nginx进程
start nginx 启动nginx
nginx -s quit 安全关闭
nginx -s stop 强制关闭
nginx -s reload 改变配置文件时,重启nginx工作进程,使配置生效
nginx -s reopen 打开日志文件
nginx -v 查看版本
nginx -h 查看帮助信息
```
####
## 4.nginx.conf
```
#全局块 :配置log日志,生成进程数等
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#配置影响Nginx服务器与用户的网络连接数,每个进程最大的连接数
events {
#当前进程的最大链接数 1024个链接
worker_connections 1024;
}
#http块中定义了多个服务,基于http块支持的请求,在一个http块中可以嵌套多个server块
http {
#加载mime.types 当前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;
#tcp_nopush on;
#keepalive_timeout 0;
#保持链接的时长
keepalive_timeout 65;
#是否打开压缩模式
#gzip on;
#server块,配置虚拟主机的相关参数,在一个http中可以有多个server
server {
#监听当前主机的端口号
listen 80;
#当前主机的名称
server_name localhost;
#编码格式
#charset koi8-r;
#日志
#access_log logs/host.access.log main;
#location块 ,配置请求的路由,以及各种页面的处理情况
location / {
#找到了根目录 html
root html;
#主页 index.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;
#}
}
#配置负载均衡的·服务器列表
#upstream edu{
#配置当前要转发的服务列表 server ip:port;
#1.负载均衡的默认规则 轮循规则 RR :配置在服务器的硬件以及网卡都一致的情况下,平均分配
#2.负载均衡的策略:权重的规则,根据服务的硬件不同,硬件越好的服务器,处理能力越强,可以将更多的请求转发到该服务器
#weight 代表请求到该服务器的权重 百分比
#3.负载均衡策略:ip_hash策略,解决session失效问题
#更具当前的请求的ip,计算出hsah码,把当前HASH码与某一个服务匹配,该ip后面访问的所有接口,都是与匹配了的服务进行交互
#ip_hash
server localhost 8082;
server localhost 8081;
#server localhost 8083 weight=9;
#server localhost 8084 weight=1;
#}
#反向代理服务器 以及负载均衡
server{
#配置要监听的端口号
listen 7000;
#主机名
server_name localhost;
location / {
#转发的服务器地址以及端口号
proxy_pass http://localhost:8082;
}
}
#静态资源服务器搭建
#通过http://localhost:8888/carsys/访问
server{
listen 8888;
server_name localhost;
#将计算机下的某个盘符,作为静态资源服务器
location /carsys/ {
root E:/carsys/;
autoindex on;
}
}
# 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;
# }
#}
}
```