Nginx配置反向代理-实现前后端完全分离

找到nginxconf ginx.conf如下部分: 找到nginxconf ginx.conf如下部分:
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
#charset koi8-r; #charset koi8-r;
#access_log logs/host.access.log main; #access_log logs/host.access.log main;
location / { location / {
root html; root html;
index index.html index.htm; index index.html index.htm;
} }
修改后如下: 修改后如下:
server { server {
listen 80; listen 80;
server_name painpointcloud; server_name painpointcloud;
#charset koi8-r; #charset koi8-r;
#access_log logs/host.access.log main; #access_log logs/host.access.log main;
#默认静态资源 #默认静态资源
location / { location / {
root html; root html;
index index.html index.htm; index index.html index.htm;
allow all; allow all;
} }
#其他动态请求反向代理到tomcat容器 #其他动态请求反向代理到tomcat容器
location ~ .(json|do)?$ { location ~ .(json|do)?$ {
index index; index index;
proxy_pass http://localhost:8080; proxy_pass http://localhost:8080;
} }
#配置[企业宣传]动态请求反向代理到tomcat容器 #配置[企业宣传]动态请求反向代理到tomcat容器
location ~ .(com)?$ { location ~ .(com)?$ {
index index; index index;
proxy_pass http://localhost:1010; proxy_pass http://localhost:1010;
} }
# 匹配任何以business开始的请求 # 匹配任何以business开始的请求
location ^~ /business/ { location ^~ /business/ {
index index; index index;
proxy_pass http://localhost:1010; proxy_pass http://localhost:1010;
} }
# 匹配任何以idea开始的请求 # 匹配任何以idea开始的请求
location ^~ /idea/ { location ^~ /idea/ {
index index; index index;
proxy_pass http://localhost:1010; proxy_pass http://localhost:1010;
} }
说明: 说明:
listen:是监听的端口,即用户访问nginx服务的端口 listen:是监听的端口,即用户访问nginx服务的端口
server_name:服务名,经过测试并不会影响到什么 server_name:服务名,经过测试并不会影响到什么
location:定义资源类型与服务器中资源地址url的映射关系,可在/后面定义资源类型,可设置多个location location:定义资源类型与服务器中资源地址url的映射关系,可在/后面定义资源类型,可设置多个location
其中proxy_pass代表要反向代理的服务器资源url,只要资源类型匹配,在这个url下的子路径资源都可以访问到, 其中proxy_pass代表要反向代理的服务器资源url,只要资源类型匹配,在这个url下的子路径资源都可以访问到,
其中root代表本地的资源路径,同样只要资源类型匹配,这个路径下的子目录资源都可以被访问到, 其中root代表本地的资源路径,同样只要资源类型匹配,这个路径下的子目录资源都可以被访问到,
一个location中只能配置一个root或proxy_pass。 一个location中只能配置一个root或proxy_pass。
修改后ngnix.conf文件后,使用nginx -s reload指令,重启ngnix,如果没有报错即重启成功 修改后ngnix.conf文件后,使用nginx -s reload指令,重启ngnix,如果没有报错即重启成功
C:Usersadmin>F: C:Usersadmin>F:
F:>cd F:developserver ginx F:>cd F:developserver ginx
F:developserver ginx>nginx -s reload F:developserver ginx>nginx -s reload
F:developserver ginx>nginx -s stop F:developserver ginx>nginx -s stop
启动Nginx:start nginx 启动Nginx:start nginx
经验分享 程序员 微信小程序 职场和发展