目录
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;

    # 解决:nginx默认request的header的那么中包含’_’时,会自动忽略掉
    underscores_in_headers on;

    # 访问入口
    server {
        listen       80;
        server_name  localhost;
	location / {
	   root /home/dist;
           index  index.html index.htm;
           proxy_http_version 1.1;
           add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
           try_files $uri $uri/ /index.html;
		   # error_page 405 =200 $1;
	}
	# 后端接口转发
	location /aaa/ {
	    proxy_pass http://192.168.21.159:8082;
	    proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
			client_max_body_size  100m;
	}
		# 后端接口转发
	location /bbb/ {
	    proxy_pass http://192.168.21.159:20021/;
	    proxy_set_header Host $host;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
			client_max_body_size  3000m;
	}
	# 重定向: 前端入口 把http://ip:80/ccc 重定向到 http://ip:83
	location ~^/ccc/(.*)$ {
	    rewrite ^/fortressFront/(.*)$ http://192.168.2.159:83/$1 permanent;
	}

    }
	# 访问入口 http://ip:83
    server {
        listen       83;
        server_name  localhost;
	# 前端配置
	location / {
           root /home/dist2/;
           index  index.html index.htm;
           proxy_http_version 1.1;
           add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
           try_files $uri $uri/ /index.html;
		   # error_page 405 =200 $1;
	}


    }
}

假设当前nginx服务器IP为 192.168.1.1

直接访问,可以进到前台;

现在请求后台 http://192.168.1.1/aaa/type/list

斜杠其实就是分隔符,会匹配 / 后面的内容。并且根据proxy_pass中是否存在斜杠,proxy_pass有斜杠就匹配的内容去除location中有的;如果没有斜杠就会把location中匹配的内容带过来。

例如这个:

location /aaa {
    proxy_pass  http://192.168.2.1:8080;
}

http://192.168.1.1/aaa/type/list 代理后的则为 http://192.168.2.1:8080/aaa/type/list

location /aaa/ {
    proxy_pass  http://192.168.2.1:8080;
}

http://192.168.1.1/aaa/type/list 代理后的则为 http://192.168.2.1:8080/aaa/type/list

location /aaa {
    proxy_pass  http://192.168.2.1:8080/;
}

http://192.168.1.1/aaa/type/list 代理后的则为 http://192.168.2.1:8080/type/list

location /aaa/ {
    proxy_pass  http://192.168.2.1:8080/;
}

http://192.168.1.1/aaa/type/list 代理后的则为 http://192.168.2.1:8080/type/list

反向代理的时候,如果头部包含 下划线 例如:access_token 需要设置一下;需放在http {} 下

    # 解决:nginx默认request的header的那么中包含’_’时,会自动忽略掉
    underscores_in_headers on;

Nginx静态资源部署问题

alias和root的区别

根路径和虚拟路径

alias 匹配的是指定的目录

root 匹配的是上级目录

例如 目录 /b/a/1.txt

配置root形式想要访问1.txt

location /a {
	root /b;
}

访问 http://localhost/a/1.txt 是可以访问到的,因为root形式是:root配置的路径 + location路径 拼接成的;

配置alias的形式访问1.txt

location /xx/ {
   alias /a/b/;  # b后面的斜线不能去掉
}

访问:http://localhost/xx/1.txt 即可访问


标题:Nginx的一些问题
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2021/08/07/1628323298509.html