vuejs - 让rails 和 vue处于同一域名,同一个端口的配置方式

栏目: Ruby · 发布时间: 7年前

内容简介:两种方式:1. 使用thin cluster: 特别简单, 参考:但是这种办法的本质上, rails是处于其他端口的。 然后在nginx上做代理转发

两种方式:

1. 使用thin cluster: 特别简单, 参考: https://stackoverflow.com/questions/49779575/running-frontend-and-backend-on-the-same-port

但是这种办法的本质上, rails是处于其他端口的。 然后在nginx上做代理转发

upstream backend {
    server 127.0.0.1:3000;
}

server {
    listen 80 default_server;

    location /api/ {
        proxy_pass         http://backend;
        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;
        proxy_set_header   X-Forwarded-Host $server_name;
    }

    location / {
        root /opt/frontend/dist;
        try_files $uri $uri/ /index.html;
    }
}

2. 对于其他服务器(例如 puma, passenger),需要:

http {
    passenger_root /usr/local/rvm/gems/ruby-2.2.10/gems/passenger-6.0.0;
    passenger_ruby /usr/local/rvm/gems/ruby-2.2.10/wrappers/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # 80 端口。
    server {
     listen 80;
     passenger_enabled on;
     # 这个是配置rails的
     root /mnt/web/rails_server/public;
     charset utf-8;
     # 配置其他的assets 
     location ~ ^/(images|javascripts|stylesheets|upload|assets|video)/  {
       root /mnt/www/rails_server/public;
       expires 30d;
       add_header Cache-Control public;
       add_header ETag "";
     }

     # vuejs .
     location /vue.html {
       root   /mnt/web/h5/dist;
     }
     location /static {
       root   /mnt/web/h5/dist;
     }

    }
}

然后,把vuejs项目中的 index.html 改成:vue.html (注意 .html不能省略) ,这样做看起来是:

1. passenger 占主要地位

2. vuejs 占次要地位

好处是:

1. 使用同一域名,不存在跨域问题

2. 可以在spa 和 ssa ( server side app )中无缝切换,共享 cookie 等。

记得vue中的各种http 请求可能需要重新修改一下路径即可。 另外编译vuejs 的时候。 弄个脚本,把 index.html都做个重命名。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

500 Lines or Less

500 Lines or Less

Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00

This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具