对后端和前端都使用Nginx路由

时间:2019-04-07 12:17:11

标签: angular spring-boot nginx port80

我已经开发了具有Angular前端和Springboot后端作为组件的应用程序。我安装了Nginx并将/ dist文件夹内容复制到/var/www/html。它工作正常,并且前端在端口80中提供服务。现在,我也需要在端口80中提供后端服务,该端口当前在8080中运行,URL中带有“ api”部分。这将允许我的后端在不使用单独端口的情况下调用api端点。这是我的/etc/nginx/sites-available/default文件。我在该文件中添加了以下部分。但是它不起作用。

   server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
            try_files $uri $uri/ /index.html =404;
        }
    }

server {
        listen 80;
        listen [::]:80;

        server_name example.com;

        location / {
             proxy_pass http://localhost:8080/;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header X-Forwarded-Port $server_port;
        }
}

1 个答案:

答案 0 :(得分:0)

`

server {
    listen 9090;
        server_name 192.168.10.10;
        index index.html;

        location /api/v1/ {
                proxy_pass http://127.0.0.1:8080; #api ip and port
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                proxy_set_header X-Real-IP     $proxy_add_x_forwarded_for;
        }
        location /{
                root /user/dist; # path of angular application upto dist
                try_files $uri $uri/ /index.html;
       }
}

`

然后点击网址http://192.168.10.10:9090