NGINX multiple server blocks with reverse proxy

2019-03-14 04:13发布

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).

and i need a little help with the nginx config. i eventually plan on running nginx on port 80 where wiki.example.com and site.example.com are the same 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.

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)

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. 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?

标签: tomcat nginx
1条回答
We Are One
2楼-- · 2019-03-14 04:52

Based on your description on what you're trying to do, I think your nginx.conf should look something like this:

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).

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.

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."

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). 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_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." 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. 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. 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").

查看更多
登录 后发表回答