Nginx配置实现前端Route路由与后端路由的分离
2017-09-01 15:06
1096 查看
Windows中Nginx的常用命令:
启动:Nginx根目录下cmd控制台
停止:Nginx根目录下cmd控制台
重启:Nginx根目录下cmd控制台
各配置项的作用和配置说明在配置中已经注释
启动:Nginx根目录下cmd控制台
start nginx.exe
停止:Nginx根目录下cmd控制台
nginx -s stop
重启:Nginx根目录下cmd控制台
nginx -s reload
注意:每次修改了Nginx.conf文件之后都必须要重启Nginx服务
下面是前后端路由分离的nginx.conf的配置,文件路径:nginx安装目录/conf/nginx.conf各配置项的作用和配置说明在配置中已经注释
#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; upstream ota_server { #配置一个服务器地址 server 127.0.0.1:8088 weight=5;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。 } server { #监听端口 listen 80; #服务器域名 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #前后端路由分离部署Nginx配置# #这里是将所有的请求全都从定向,关于permanent,last等参数请参考http://blog.csdn.net/zhanlanmg/article/details/49684803 #rewrite后面第一个参数为正则表达式,第二个参数是重写的URL,正则表达式中每一段(用“()”包起来的)按先后顺序在第二个参数中引用为$1 $2一次类推 #例如 rewrite ^/([a-z]*)/([0-9])/(.*)$ /param-$1/value-$2/$3的规则转发 /age/13/zhangsan 重写后的url为 /param-age/value-13/zhangsan rewrite ^/(.*)/$ /$1 permanent; #设置默认页面为index.html index index.html; #设置应用的根路径为nginx目录中的html目录下的umsapp-ota root html/umsapp-ota; #结合上诉配置,但我们在浏览器中输入localhost的时候,请求的是:nginx根目录/html/umsapp-ota/index.html #这里根据后端请求的路由规则进行过滤匹配,比如我在项目中将所有ajax请求的地址前面都增加了一个/api,然后这里就可以根据/api进行过滤了 location /api { #rewrite break - url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求,地址栏url不变 请参考http://blog.csdn.net/zhanlanmg/article/details/49684803 #这里前端ota的请求中包含了api,所以将api去掉,用来匹配后台的服务器资源,主要用于将前后端的url分离 #如果是tomcat启动多应用模块,需要增加应用的名称。 #例如tomcat的web根目录webapp中配置了2个应用A_Server,B_Server,如果我们要转发到B_Server,那么我们的配置应该是rewrite ^/api/(.*)$ /B_Server/$1 break; rewrite ^/api/(.*)$ /$1 break; #转发的域名地址,ota_server上前面upstream配置的服务器地址 proxy_pass http://ota_server; #上述配置生效后,但我们点击OTA-WEB页面中的“APP管理”按钮时,浏览器的地址会变成http://localhost/#/app/list/1 #同时js代码中的ajax请求会以相对路径的形式发送请求/api/api-group/list?start=0&limit=10&page=1来查询数据 #(完整路径是:http://localhost/api/api-group/list?start=0&limit=10&page=1), #由于配置了上述过滤器这个ajax的请求就会变成 http://ota_server/api-group/list?start=0&limit=10&page=1,但是浏览器的地址保持不变(rewrite break的作用) #从而实现了前后端路由请求的分离。 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ###################################################################################################################################################### #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; 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 # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
相关文章推荐
- Nginx部署前端代码实现前后端分离
- Nginx配置反向代理-实现前后端完全分离
- 通过nginx部署前端代码实现前后端分离
- nginx前后端分离的配置
- Ansible 一键配置安装Keepalived+Nginx作为前端,httpd+php作为后端
- windows server,nginx安装,配置,运行nodeJS后端的web项目的实现,以及错误分析及解决方法
- 前后端分离配置篇(vue+webpack+mock+nginx+spring)
- nginx+vue.js实现前后端分离的示例代码
- nginx反向代理实现前后端分离&跨域问题
- 前端视角漫谈百度ueditor编辑器前后端分离配置
- mvc中Url.RouteUrl或者Html.RouteLink实现灵活超链接,使href的值随路由名称或配置的改变而改变[bubuko.com]
- 【 Linux 】lvs-dr模型实现HA,后端Nginx、PHP、MySQL分离 搭建wordpress站点
- nginx配置反向代理解决前后端分离跨域问题
- web前端--实现前后端分离的心得
- 一个完全由后端人员写的前端页面怎么实现前后端分离?
- 配置Nginx前端Apache后端服务器LNMPA-与LNMP,HHVM性能比拼
- centos7 nginx前端 apache后端 及多站点简单配置
- 前后端分离,nginx配置解决js、css无法加载
- 前后端分离 单点登录SSO 纯前端实现单点登录SSO
- nginx反向代理tomcat、实现动静分离/配置文件