# Nginx部署笔记-常用配置总结
最近部署
nginx的小总结,做个记录。
# 一、服务端口代理跳转第三方地址
项目中配有除本地服务地址外的第三方服务地址,假设其地址为https://www.example.com,那么访问http://localhost:8000/todo/xxx将会被代理至https://www.example.com/todo/xxx。
http {
    # 其他省略
    server {
        listen       8000; # 监听端口
        server_name  localhost; # 前端地址
        root   project; # 前端项目文件目录
        location ^~/todo/{
            rewrite /(.*)$ /$1 break;
            proxy_pass https://www.example.com;# 跳转第三方地址
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 二、登录验证+跳转至登录页
适用场景:用户跳过登录页直接访问http://localhost:8000/System/html。
如果之前没有登录过/Cookie过期失效,就会跳转至http://localhost:8000/login.html登录页,
若想要在登录页成功登陆之后跳转回原来想要访问的地址,从Cookie中获取就好了。
http {
    # 其他省略
    server {
        listen       8000; # 监听端口
        server_name  localhost; # 前端地址
        root   project; # 前端项目文件目录
        location ^~/System/html{
            auth_request /auth;
            error_page 401 = @error401;# 401为没有访问权限/需进行登录验证
        }
        location /auth {# 用户登录验证
			internal;# 该location只能被内部调用,外部访问无效->404
            proxy_set_header Host $host;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_pass http://localhost:8001/LoginVerification;# 用户登录验证地址
		}
        location @error401 {
            add_header Set-Cookie "redirect=$scheme://$http_host$request_uri;Path=/";# 将当前访问地址设置为Cookie
            set $page_401 http://localhost:8000/login.html;# 登录页面
            return 302 $page_401;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 三、API端口代理
假设前端项目端口为8000,而服务端口则是8001,配置如下↓
http {
    # 其他省略
    server {
        listen       8000; # 监听端口
        server_name  localhost; # 前端地址
        root   project; # 前端项目文件目录
        location ^~/api/{
            proxy_pass http://localhost:8001;# 跳转端口
            proxy_set_header X-real-ip $remote_addr;# 发出请求的IP
            proxy_set_header Host $http_host;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 四、快捷访问
假设当前访问192.168.1.1,直接跳转至192.168.1.1/example/index.html以实现快捷访问。
使用配置:
server {
    location /{
        index /example/index.html;
        try_files $uri /$uri /example/$uri /example/index.html;
    }
}
1
2
3
4
5
6
2
3
4
5
6
