tomcat的nginx代理

tomcat的nginx代理,nginx,tomcat,reverse-proxy,Nginx,Tomcat,Reverse Proxy,我意识到它更多地属于serverfault,所以我把它移到了那里: 我看了几十本教程,但我无法理解以下内容: 我在/var/www/mydomain.com中有已编译的vue应用程序,我希望它作为静态内容共享 我的后端由tomcat运行在8080上,公共api位于/api/something上。。。网址。URL是硬编码的,包括“api”部分 我想将nginx配置为代理mydomain.com/api/something。。。对tomcat和rest的请求可以从/var/www/mydomain.

我意识到它更多地属于serverfault,所以我把它移到了那里:

我看了几十本教程,但我无法理解以下内容:

我在/var/www/mydomain.com中有已编译的vue应用程序,我希望它作为静态内容共享

我的后端由tomcat运行在8080上,公共api位于/api/something上。。。网址。URL是硬编码的,包括“api”部分

我想将nginx配置为代理mydomain.com/api/something。。。对tomcat和rest的请求可以从/var/www/mydomain.com静态提供。一切都是通过SSL提供的

我几乎不需要别的东西

你能帮我配置nginx和tomcat来实现这一点吗?谢谢大家!

nginx config/etc/nginx/sites available/mydomain.com

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
        listen 443 ssl default_server;
        #listen [::]:443 ssl default_server;

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

       server_name _ mydomain.com www.mydomain.com;

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

        location /api/ {
                include proxy_params;
                proxy_pass http://tomcat;
        }

        ssl_certificate /etc/letsencrypt/live/www.mydomain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.mydomain.com/privkey.pem; # managed by Certbot
}
server {
    if ($host = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


       listen 80 default_server;
        listen [::]:80 default_server;

       server_name _ mydomain.com www.mydomain.com;
   return 404; # managed by Certbot
}
Tomcat config server.xml

        <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               address="127.0.0.1"
               redirectPort="8443" />


当前的情况是,当我转到mydomain.com/api/docs,swagger应该在那里运行时,我被重定向回mydomain.com,或者得到500或502错误。

proxy\u pass
命令中删除结束斜杠:它应该是
proxy\u passhttp://tomcat
。没有
/
它不会更改URI路径,使用
/
它将在将请求转发到Tomcat之前删除
/api
前缀。谢谢。我删除了它,但是当我转到mydomain.com/api/docs时,它仍然会将我重定向到mydomain.com。你可以检查nginx和Tomcat访问日志,看看是哪一个发送了重定向。我在这两个地方都看不到。当我访问mydomain.com时,它位于/var/log/nginx/log中。当我转到…/api/docs时,它不在那里,也不在/var/log/tomcat9/localhost\u access\u log.2021-01-29.txt删除
listen 127.0.0.1
指令,它告诉第一个
服务器
侦听
127.0.0.1:80