tomcat - 具有反向代理的 NGINX 多服务器 block

标签 tomcat nginx

我在 Windows 上运行 nginx+PHP+MySQL 和 tomcat+postresql(我知道这不是很好地利用资源,我只需要它们用于某些项目)。

我需要一些关于 nginx 配置的帮助。 我最终计划在端口 80 上运行 nginx,其中 wiki.example.com 和 site.example.com 是相同的 IP。 但我想将对 site.example.com 的请求转发到位于 localhost:8080 的 tomcat,而对 wiki.example.com 的请求将由 nginx 提供服务。

我知道我的问题出在配置上,只是因为我可以在控制台中看到错误;我无法设置两个“位置/”设置,即使它们位于不同的服务器 block 中。这是我的配置,有人知道如何修复吗?

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

server {
listen 8080 default_server;
server_name _;
server_name_in_redirect off;
root  www/html;
}

server {
    listen       wiki.example.com:8080;
    server_name  wiki.example.com;

    location / {
        #proxy_pass http://localhost:80/;
        #proxy_set_header  Host $http_host;
        root   www/wiki;
        index  index.html index.htm index.php;
    }

    location ~ .php$ {
        root           www/wiki;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
        index index.php;
    }

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

server {
    listen site.example.com:8080;
    server_name site.example.com;
    root www/html;

    location / {
        proxy_pass http://localhost:80/;
        proxy_set_header  Host $http_host;
        root   www/html;
        index  index.html index.htm index.php;
    }

    }

}

编辑:

非常感谢您的帮助。我已经按照你的指示编辑了配置,它大部分都有效!! :) 导航到 site.example.com 将加载站点(由 nginx 代理并通过 tomcat 提供服务)

但是 导航到 wiki.example.com 会产生一个 nginx 404 消息,但是导航到 wiki.example.com/index.php?title=Main_Page 会正常加载 mediawiki。 所以似乎 wiki 服务器 block 的默认值被搞乱了。

http://pastebin.com/znT8q6WQ

这看起来像错误的部分吗?

最佳答案

根据您对您尝试执行的操作的描述,我认为您的 nginx.conf 应该如下所示:

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

    server {
        listen 80;
        server_name site.example.com;

        # pass all requests to service listening on port 8080

        location / {
            include /usr/local/nginx/conf/proxy.conf;
            proxy_pass http://localhost:8080;
            proxy_redirect http://localhost:8080/ http://$host;
        }
    }

    server {
        listen 80;
        server_name wiki.example.com;
        root /path/to/your/www/wiki;
        index index.html index.htm index.php;

        location / {
            try_files $uri @backend;
        }

        location @backend {
            rewrite ^ /index.php?title=$request_uri last;
        }

        # proxy to php-fpm listening on port 9000

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # your other rules here...

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

    }

    server {
        listen 80 default_server;
        server_name _;
        access_log off;
        return 301 $scheme://site.example.com$request_uri;
    }
}

在第一个服务器 block 中,nginx 将在端口 80 上监听与“site.example.com”匹配的请求并代理到您在 8080 (tomcat) 上运行的任何服务。

在第二个服务器 block 中,nginx 将在端口 80 上监听与“wiki.example.com”匹配的请求,并使用 php(或者可能是 php-fpm)在端口 9000 上监听来处理它们。

最后一个服务器 block 是您的默认值。它还在端口 80 上监听并作为一个包罗万象——任何与“site.example.com”或“wiki.example.com”不匹配的内容都将在这里结束,在示例中它只是重定向到“site.example. com。”

附加信息

代理到后端服务的位置 block 包含一个 include 语句,它在 nginx conf 目录 (/usr/local/nginx/conf) 中加载一个“proxy.conf”。这是我用来指定代理参数的一组“proxy.conf”配置示例——如果您不使用单独的配置文件,则可以内联指定这些配置:

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 32m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k; 

编辑:

更新了“wiki.example.com”的服务器 block 代码示例。为清楚起见,我建议您将 root 目录指定为绝对路径,这样您和 nginx 就知道在文件系统上的确切位置。此外,在更新后的代码中,@backend block 将重定向所有 nginx 找不到匹配项的请求,并将其传递给媒体维基 index.php 文件进行处理。这还允许您使用干净的 URL,例如“wiki.example.com/Main_Page”(这将被重写为“wiki.example.com/index.php?title=Main_Page”)。

关于tomcat - 具有反向代理的 NGINX 多服务器 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902727/

相关文章:

php7.0-fpm高负载cpu

database - 无法解析字符串值中的占位符 'database.driverClassName'

java - 如何正确删除catalina.out?

apache - 避免 JKMount,使用 mod_rewrite 而不是 mod_jk

python - uwsgi + django 通过 Nginx - uwsgi 设置/生成?

ruby - nginx 背后的 Unicorn vs Passenger Standalone

java - 创建一个 web UI 来编辑发送邮件的别名文件

java - Hazelcast HTTP session 复制不复制任何内容

c - Nginx 模块获取客户端 ssl 证书

networking - Golang反向代理到nginx背后的app