nginx反向代理+ Spring ResourceSupport生成错误的URL路径前缀

时间:2016-04-19 23:11:52

标签: spring spring-mvc nginx spring-hateoas maven-tomcat-plugin

Spring HATEOAS ResourceSupport在其响应中生成错误的URL。我使用Tomcat和Spring以及NGINX作为反向代理。

Spring生成的网址:http://localhost:8080/spring-ng-seed

项目网址:https://spring-ng-seed.dev/(提供静态内容),

Web API网址:https://spring-ng-seed.dev/wapi/

/wapi/的所有请求都正常工作但Spring HATEOAS的ResourceSupport生成的网址如下所示: https://spring-ng-seed.dev/spring-ng-seed/foo/bar 代替 https://spring-ng-seed.dev/wapi/foo/bar

例如,当我们调用https://spring-ng-seed.dev/wapi/foo/bar时,我们最终会以https://spring-ng-seed.dev/spring-ng-seed/foo/bar作为self rel返回,这是不正确的。

/spring-ng-seed/foo/bar在响应链接中应为/wapi/foo/bar

我不确定配置错误,nginx,tomcat或spring但我在其他地方找不到任何东西。

我也在前端使用AngularJS,但我怀疑问题在于前端,而是将nginx作为反向代理或tomcat。

有人可以帮忙吗?

NGINX配置:

server {
    charset UTF-8;
    listen 80;
    listen 443 ssl;

    server_name www.spring-ng-seed.dev spring-ng-seed.dev;

    ssl on;
    ssl_certificate           /Users/reecefowell/Projects/Spring/spring-ng-seed/cert.pem;        # path to your cacert.pem
    ssl_certificate_key       /Users/reecefowell/Projects/Spring/spring-ng-seed/server.key;    # path to your privkey.pem
    ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers               ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location /wapi/ {   
        proxy_pass               http://localhost:8080/spring-ng-seed/;
        proxy_read_timeout       90;
        proxy_connect_timeout    90;
        # proxy_ssl_session_reuse off;
        proxy_redirect off;

        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Prefix $http_x_forwarded_prefix;
        proxy_set_header X-Forwarded-Host $http_x_forwarded_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_set_header X-Forwarded-Protocol https;
        proxy_set_header X-Forwarded-Ssl on;
        proxy_set_header X-Url-Scheme https;
    }

    location / {
        root /Users/reecefowell/Projects/Spring/spring-ng-seed/src/main/webapp/app/build;

        index index.html;
    }
}

我的Tomcat是通过我的pom.xml文件配置的:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <groupId>spring-ng-seed</groupId>
    <artifactId>spring-ng-seed</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.0.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
            <version>0.12.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.0</version>
        </dependency>

        ...

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <!--<configuration>-->
                    <!--<url>http://localhost:8080/manager</url>-->
                    <!--<server>localhost</server>-->
                <!--</configuration>-->
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
            </plugin>
        </plugins>
    </build>
</project>

1 个答案:

答案 0 :(得分:1)

您可以查看请求在到达您的服务时包含的代理标头。 spring-hateoas将处理x-forwarded-prefix标题并将其添加到您的路径中。

在这里看看这个答案: https://stackoverflow.com/a/36163242/5371736