【问题标题】:NGINX in front of TOMCAT to serve static filesTOMCAT前面的NGINX服务静态文件
【发布时间】:2014-04-08 09:05:53
【问题描述】:

我正在使用 grails 2.2.2 在本地计算机上进行项目 Mac OSX Lion 10.7.5 我已经安装了 NGINXbrew 并修改了 nginx.conf 如下:

worker_processes  1;
error_log  logs/error.log  info;

events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8081;
        server_name  localhost;

    root /;

    access_log /Users/lorenzo/grails/projects/logs/myproject_access.log;

        location / {
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://localhost:8081;
        }

    #images folders
    location /posters {
        root /Users/lorenzo/grails/projects/posters/;   
    }

    #images folders
    location /avatars {
        root /Users/lorenzo/grails/projects/avatars/;
    }

    #images folders
    location /waveforms {
        root /Users/lorenzo/grails/projects/waveforms/;     
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

当我访问http://localhost:8081 时,我的网站正在运行,但我想确保imagesnginx 提供服务,而不是由tomcat 提供服务,所以我查看myproject_access.log,但没有任何反应。

ngnixtomcat运行时写入日志。

有没有办法“监控”nginx 提供的静态文件?

谢谢

编辑

Executing curl -I http://localhost:8081

tomcat 运行时输出为:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1    //TOMCAT
...

当 tomcat 未运行时,输出为:

HTTP/1.1 500 Internal Server Error
Server: nginx/1.4.1   //NGINX
Date: Tue, 08 Apr 2014 09:30:00 GMT
Content-Type: text/html
Content-Length: 192
Connection: keep-alive 

【问题讨论】:

    标签: macos tomcat grails nginx


    【解决方案1】:

    你的问题是你让两台服务器在同一个端口上监听,你需要将 tomcat 移动到另一个端口,如 8082 并让 nginx 监听主端口(在你的情况下是 8081 ),然后告诉 nginx 在请求不是图像(或任何资产)时代理到8082

    这也是对您的服务器块的改进

    server {
      server_name localhost;
      listen 8081;
      root /Users/lorenzo/grails/projects;
      location @tomcat {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8082;
      }
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root   html;
      }
      location / {
        try_files $uri $uri/ @tomcat;
      }
    }
    

    【讨论】:

    • 感谢您的回答!我正在尝试您的解决方案,但仍然不完全理解发生了什么。在我的浏览器上,我应该使用 localhost:8081 还是 8082 ?对不起,如果这是一个愚蠢的问题,但我有点迷茫..我仍然不知道如何查看静态文件是由 nginx 还是 tomcat 提供的
    • 8082 将是 tomcat 服务器,8081 将是 nginx 服务器,您将像现在一样正常编写 8081,服务器将决定它是否是对可以正确传递的东西的请求像图像或css文件或js文件一样,如果不是nginx,则将请求传递给tomcat并等待tomcat回复并将回复传递给用户
    • 8081 指向 nginx,8082 指向 tomcat,8082 可以正常工作,但 8081 出现错误 500。
    • 如果你能把错误日志中的错误告诉我,那会很有帮助
    • 也可以试试sudo nginx -t,如果有错误,把输出粘贴给我
    猜你喜欢
    • 2019-07-01
    • 2016-04-05
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多