[英]NGINX multiple server blocks with reverse proxy
I am running nginx+PHP+MySQL, and tomcat+postresql on windows (i know its not a very good use of resources, i just need them all for certain projects). 我正在运行nginx + PHP + MySQL,以及windows上的tomcat + postresql(我知道它不是很好用的资源,我只需要它们用于某些项目)。
and i need a little help with the nginx config. 我需要一点nginx配置的帮助。 i eventually plan on running nginx on port 80 where wiki.example.com and site.example.com are the same IP. 我最终计划在端口80上运行nginx,其中wiki.example.com和site.example.com是相同的IP。 but i would like to forward requests for site.example.com to tomcat at localhost:8080, and requests for wiki.example.com will be served by nginx. 但我想将site.example.com的请求转发到localhost:8080的tomcat,并且nginx将为wiki.example.com提供服务。
i know where my problem lies in the config, only because i can see the error in the console; 我知道我的问题在于配置,只是因为我可以在控制台中看到错误; i cant set two "location /" settings even if they are in different server blocks. 即使它们位于不同的服务器块中,我也无法设置两个“位置/”设置。 here is my config, does anyone know how to fix it? 这是我的配置,有谁知道如何解决它?
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;
}
}
}
EDIT: 编辑:
thank you very much for your help. 非常感谢您的帮助。 i have edited the config per your instruction and it mostly works!! 我根据你的指示编辑了配置,它主要工作! :) navigating to site.example.com will load the site (proxied by nginx and served via tomcat) :)导航到site.example.com将加载该站点(由nginx代理并通过tomcat提供)
BUT navigating to wiki.example.com will produce an nginx 404 message, but navigating to wiki.example.com/index.php?title=Main_Page will load mediawiki as normal. 但导航到wiki.example.com将生成nginx 404消息,但导航到wiki.example.com/index.php?title=Main_Page将正常加载mediawiki。 so it seems that the defaults are messed up for the wiki server block. 所以似乎默认值是为维基服务器块搞砸了。
http://pastebin.com/znT8q6WQ
does this look like the part with the error? 这看起来像错误的部分?
Based on your description on what you're trying to do, I think your nginx.conf should look something like this: 根据你对你要做的事情的描述,我认为你的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;
}
}
In the first server block, nginx will listen on port 80 for requests matching "site.example.com" and proxy to whatever service you're running on 8080 (tomcat). 在第一个服务器块中,nginx将在端口80上侦听与“site.example.com”匹配的请求,并代理您在8080(tomcat)上运行的任何服务。
In the second server block, nginx will listen on port 80 for requests matching "wiki.example.com" and use php (or presumably php-fpm) listening on port 9000 to process them. 在第二个服务器块中,nginx将在端口80上侦听与“wiki.example.com”匹配的请求,并使用php(或大概是php-fpm)侦听端口9000来处理它们。
The final server block is your default. 最终的服务器块是您的默认值。 It's also listening on port 80 and serves as a catchall -- whatever doesn't match "site.example.com" or "wiki.example.com" will end up here and in the example it just redirects to "site.example.com." 它还在端口80上进行监听并充当一个笼罩 - 任何不匹配的“site.example.com”或“wiki.example.com”将在此处结束,在示例中它只是重定向到“site.example”。 COM“。
Additional info 附加信息
The location block above that proxies to a backend service contains an include statement that loads a "proxy.conf" in the nginx conf directory (/usr/local/nginx/conf). 代理后端服务的上面的位置块包含一个include语句,用于在nginx conf目录(/ usr / local / nginx / conf)中加载“proxy.conf”。 Here is an example "proxy.conf" set of configurations I use to specify the proxy parameters -- you can specify these inline if you do not use a separate configuration file: 下面是我用来指定代理参数的“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;
EDIT: 编辑:
Updated server block code example for "wiki.example.com." 更新了“wiki.example.com”的服务器块代码示例。 I would recommend for clarity that you specify your root directory as an absolute path so you and nginx know exactly where to find it on the file system. 为了清楚起见,我建议您将根目录指定为绝对路径,以便您和nginx确切知道在文件系统上找到它的确切位置。 Also, in the updated code, the @backend block will redirect and pass all requests that nginx cannot find a match for to the media wiki index.php file to process. 此外,在更新的代码中, @ backend块将重定向并将所有nginx无法找到匹配的请求传递给要处理的media wiki index.php文件。 This also allows you to use clean URLs like "wiki.example.com/Main_Page" (and that will get rewritten as "wiki.example.com/index.php?title=Main_Page"). 这也允许您使用干净的URL,如“wiki.example.com/Main_Page”(并将被重写为“wiki.example.com/index.php?title=Main_Page”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.