Nginx将'/ admin / *'URL路由到后端应用程序

时间:2019-01-08 14:44:50

标签: nginx

我在同一台主机上有一个用VueJS编写的Rails应用程序和一个前端应用程序。它们被配置为使用相同的Nginx服务器。我可以在浏览器中以IP 1.2.3.4访问前端应用程序而没有问题,但是我的rails应用程序中也有/admin/*条路由,现在,当我访问1.2.3.4/admin时,它显然不起作用,因为我正在尝试访问我的前端应用程序中的/admin页。

现在,是否可以配置Nginx将请求从1.2.3.4/admin/和所有其他请求(例如/admin/posts路由到我的后端应用程序?

这是我的nginx.conf文件。

http {

  passenger_root /home/deploy/.rvm/gems/ruby-2.5.1/gems/passenger-5.3.5;
  passenger_ruby /home/deploy/.rvm/gems/ruby-2.5.1/wrappers/ruby;

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

  # Here is my rails app server config
  server {
    listen       3000;
    server_name  bikeramp.local;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    root /home/deploy/bikeramp/current/public;
    passenger_enabled on;
    rails_env production;
    access_log /var/log/nginx/bikeramp.access.log;

    # Here's my VueJS app server config
  server {
    listen       80 default_server;
    server_name  bikeramp_front.local;

    location / {
        root   /home/jdomanski/bikeramp-front;
    try_files $uri $uri/ /index.html;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

http {

passenger_root /home/deploy/.rvm/gems/ruby-2.5.1/gems/passenger-5.3.5;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.5.1/wrappers/ruby;

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

# Here is my rails app server config
server {
listen       3000 default_server; # default for port 3000
server_name  bikeramp.local 1.2.3.4;

#charset koi8-r;

#access_log  logs/host.access.log  main;

root /home/deploy/bikeramp/current/public;
passenger_enabled on;
rails_env production;
access_log /var/log/nginx/bikeramp.access.log;

# Here's my VueJS app server config

}
  server {
  listen       80 default_server; #default for port 80
  server_name  bikeramp_front.local;

  location /admin/ {
     proxy_pass http://localhost:3000; #in the backend sector, youlisten to port 3000, not 80, so you need to redirect the specific path you want there. This should kinda work.
  }
  location / {
      root   /home/jdomanski/bikeramp-front;
  try_files $uri $uri/ /index.html;
  }

}

唯一侦听端口80(http连接,这是您想要的)的服务器块是前端服务器。从那里,您在/ admin / *上放置一个位置,并将所有流量重定向到端口3000,我在其中看到您的后端应用程序服务器块在监听。代码是否干净清晰?我可以尝试重新格式化。

https://www.linode.com/docs/development/ror/ruby-on-rails-nginx-debian/