如何将nignx代理根路径配置为tomcat服务器,但同时直接为静态内容提供服务

时间:2017-07-05 09:41:01

标签: nginx

我希望nginx直接提供http://myhost.com/v2 staic内容,

但代理http://myhost.com/http://mytomcat:8080/

我的/ etc / nginx / sites-enabled / default

的配置
   location /v2/static {

    }

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
            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_pass http://127.0.0.1:8080/;
    }

现在,在访问http://myhost.com/时获得了HTTP ERROR 404,但http://mytomcat:8080/正在运行,

任何人都可以告诉我如何配置nginx?

1 个答案:

答案 0 :(得分:1)

设置port_in_redirect off;完全按以下方式设置配置:

location / {   
        proxy_pass http://127.0.0.1:8080/;
        port_in_redirect off;    
        proxy_redirect  off;
        proxy_set_header  Host  $http_host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
}

希望有所帮助