NGINX具有反向代理的多个服务器块

如何解决NGINX具有反向代理的多个服务器块

根据您对要执行的操作的描述,我认为您的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;
    }
}

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

在第二个服务器块中,nginx将在端口80上侦听与“ wiki.example.com”匹配的请求,并使用php(或者大概是php- fpm)在端口9000上侦听它们。

最终服务器块是您的默认服务器块。它还在端口80上侦听,并作为一个包包-与“ site.example.com”或“wiki.example.com”不匹配的内容都将在此处结束,在示例中,它仅重定向到“ site.example”。 com。”

后端服务代理上方的位置块包含一个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”的服务器块代码示例。为了清楚起见,我建议您将 目录指定为绝对路径,以便您和nginx确切知道在文件系统上的哪里可以找到它。另外,在更新的代码中, 块将重定向并将nginx找不到匹配项的所有请求传递给要处理的媒体wiki index.php文件。这也允许您使用干净的URL,例如“wiki.example.com/Main_Page”(并且将被重写为“wiki.example.com/index.php?title=Main_Page”)。

解决方法

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

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

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

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服务器块的默认值混乱了。

http://pastebin.com/znT8q6WQ

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?