nginx小结--请求转发与动静分离

博主最近在使用nginx的时候,应该电脑中的nginx安装了两个版本,导致配置的时候搞到怀疑人生,我是不是用了个假的nginx,特此做好使用记录,以便下次不会处理傻逼问题

一,首先是nginx的基本逻辑图

nginx小结--请求转发与动静分离

二.nginx相关命令

1、启动:start nginx或 ./nginx.exe
2、停止:nginx -s stop (快速停止) 或 nginx-s quit(完整有序的停止)
3、重新载入配置:Nginx -s reload
4、重新打开日志文件nginx -s reopen
3、配置文件检查:Nginx -t
5、查看版本:nginx -v
6、杀死所有nginx进程:killall nginx

三.配置文件基本解释

现在先以官方的配置文档为例,作为参考的基本配置


#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;#程序的pid,用于指定pid

events {
worker_connections 1024; #nginx的最大连接数
}


http {
include mime.types; #引用同目录下自带的配置,这个配置用于设置http请求类型对应的contentType
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; #nginxde访问日志

sendfile on; #特殊的数据传输功能
#tcp_nopush on; #转发是否延迟
#tcp_nodelay on; #转发是否延迟,no为不延时,直接转发

keepalive_timeout 65; #连接保存时间

#gzip on; #数据请求是否压缩

server {
listen 8081; #监听端口号
server_name localhost; #主机名,可以不设

#charset koi8-r;#设定返回的编码方式.

#access_log logs/host.access.log main; #本服务的请求日志

location / { #请求转发的路径"/"
root html; #目标文件路径(默认使用nginx下的html文件),root的处理结果是:root路径+location路径,加不加“/”都可以
#alias path/; #别名配置,用法同上面的root很像 ,alias的处理结果是:使用alias路径替换location路径,路径最后必须带上“/”
index index.html index.htm;
}
# 示例(关于location的匹配语法,参考https://www.cnblogs.com/jiangyang/p/8485046.html)
#location /test/ {
# alias /home/test/;
#}
#访问http://www.test.com/test/aaa.html 实际为 /home/test/aaa.html

#改为root方式:
#location /test/ {
# root /home/;
#}
#访问http://www.test.com/test/aaa.html 实际为 /home/test/aaa.html

#error_page 404 /404.html; #404页面

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #500页面,又几种500页面,都支持配置
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
# http 服务设置
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem; #https证书
# ssl_certificate_key cert.key; #https密钥

# ssl_session_cache shared:SSL:1m; #cache的存储时间
# ssl_session_timeout 5m; #超时时间

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

四.实现静态文件的转发和定向

  静态文件转发相当于将nginx当成资源服务器,只需要将nginx的转发路径与服务器的本地路径匹配起来,下面试一个简单示例(仅以ocation内容)

location /yourpage { #需要转发的路径"/youpange"
    root youdir; #目标文件路径(本机),“/youdir”,也可以是相对路径
    index index.html index.htm;
  }

作为静态服务器还是相当简单的

五.动态代理

动态代理将nginx当成请求转发的工具,下面作为一个简单案例,实现按权重的负载均衡(默认轮询,还有iphash和服务器均衡fair)

upstream yourproxyserver{  # 配置你需要的代理的服务器组,名字叫yourproxyserver

  server1  192.168.128.119:8080 weight=1;   #服务器1,名字任取权重为

  server1  192.168.128.119:8080 weight=9;  #服务器2,名字任取,权重为
  }
  location /yourpage { #需要转发的路径"/youpange"
    proxy_pass http://yourproxyserver;  # 代理上面的yourproxyserver

  proxy_set_header Host $host;  # 设置请求头

  proxy_set_header X-Real-IP $remote_addr; 设置真实id

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #设置转发的信息

  #等等

}

八.踩坑记录

1.关于nginx会自动屏蔽掉请求头中带‘_‘(短下划线)字符的头信息,以前搞鉴权就被坑过,所以请求头的key不要使用"user_token",而要使用"user-token"

附:该文章含有的干货比较多 https://blog.csdn.net/feifuzeng/article/details/78437698