nginx配置浅析

nginx配置浅析,第1张

nginx配置浅析

nginx的安装在此不在赘述,可参加官网或相关教程
对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;

events {
    worker_connections  1024;
}

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;

    #配置支持websocket
    map $http_upgrade $connection_upgrade{
        default upgrade;
        ''      close;
    }
    
    upstream hello_server {
        server 10.0.0.1:8081 max_fails=3 fail_timeout=30s weight=3;
        server 10.0.0.2:8082 max_fails=3 fail_timeout=30s weight=2;
        server 10.0.0.3:8083 max_fails=3 fail_timeout=30s weight=1;
    }
    
    upstream socket_server {
        ip_hash;
        server localhost:9001;
        server localhost:9002;
        server localhost:9003;
    }
    
    #配置全局跨域
    #add_header 'Access-Control-Allow-Origin' '*';
    #add_header 'Access-Control-Allow-Credentials' 'true';
    #add_header 'Access-Control-Allow-Methods' 'GET,OPTIONS,POST';
    #add_header 'Access-Control-Allow-Headers' '*';
    
    server {
        listen       80;
        #访问地址
        server_name  192.168.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        if ($request_method = 'OPTIONS') {
               return 200;
        }

        location / {
            #前端打包存放位置
            root   html/hello;
            index  index.html index.htm;
        }
        
        location /hello{
         	 #配置局部跨域
   			 #add_header 'Access-Control-Allow-Origin' '*';
   			 #add_header 'Access-Control-Allow-Credentials' 'true';
   			 #add_header 'Access-Control-Allow-Methods' 'GET,OPTIONS,POST';
   			 #add_header 'Access-Control-Allow-Headers' '*';
    
            proxy_pass http://hello_server;
            proxy_set_header Host $host;
            proxy_set_header X_Real_Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #配置支持websocket
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
        
        location /socket {
            proxy_pass http://socket_server;
            proxy_set_header Host $host;
            proxy_set_header X_Real_Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #配置支持websocket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        #静态资源
        location /images/ {
            root /data/www/;
        }

}
#路由匹配规则
location /abcd { […] }
匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式
http://website.com/abcd # 正好完全匹配
http://website.com/abcd/ # 末尾存在反斜杠(trailing slash)也属于匹配范围内
http://website.com/abcde # 仍然匹配,因为 URI 是以 pattern 开头的

在nginx中配置proxy_pass时,
* 当在后面的url加上了/,相当于是绝对根路径,则nginx会把location中匹配的‘路径部分’代理走;
* 如果没有/,没有路由,则不会把匹配的‘路径部分’给代理走;如果没有/,有路由,则会把匹配的‘路径部分’给代理走;
访问路径:http://127.0.0.1/proxy/cuffs/css/toosimple.txt
1、绝对路径
location /proxy/ {
  proxy_pass http://10.0.0.1:8080/;
}
代理路径:http://10.0.0.1:8080/cuffs/css/toosimple.txt
location /proxy/ {
  proxy_pass http://10.0.0.1:8080/static01/;
}
代理路径:http://10.0.0.1:8080/static01/cuffs/css/toosimple.txt

2、相对路径
location /proxy/ {
  proxy_pass http://10.0.0.1:8080;
}
代理路径:http://10.0.0.1:8080/proxy/cuffs/css/toosimple.txt
location /proxy/ {
  proxy_pass http://10.0.0.1:8080/static01;
}
代理路径:http://10.0.0.1:8080/static01cuffs/css/toosimple.txt

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5671982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存