Nginx配置实现前端Route路由与后端路由的分离,nginxroute


Windows中Nginx的常用命令:
启动: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;
    #    }
    #}

}

相关内容

    暂无相关文章