我运行nginx + PHP + MySQL,和Windows上的tomcat + postresql(我知道它不是一个很好的资源使用,我只是需要他们所有的某些项目)。
我需要一点帮助与Nginx的configuration。 我最终计划在端口80上运行nginx,其中wiki.example.com和site.example.com是相同的IP。 但是我想将site.example.com的请求转发到localhost:8080的tomcat,并且nginx会提供对wiki.example.com的请求。
我知道我的问题在configuration中,只是因为我可以看到在控制台中的错误; 我不能设置两个“位置/”设置,即使他们在不同的服务器块。 这里是我的configuration,有谁知道如何解决它?
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; } } }
编辑:
非常感谢您的帮助。 我已经编辑了你的指令configuration,它大多工作! :)导航到site.example.com将加载网站(由nginx代理和通过tomcat服务)
但导航到wiki.example.com将产生一个nginx 404消息,但导航到wiki.example.com/index.php?title=Main_Page将正常加载mediawiki。 所以似乎默认是搞砸了维基服务器块。
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; } }
在第一个服务器块中,nginx将监听端口80上是否存在与“site.example.com”匹配的请求,以及代理服务器是否在8080(tomcat)上运行的任何服务。
在第二个服务器模块中,nginx会在端口80上侦听匹配“wiki.example.com”的请求,并使用php(或者可能是php-fpm)监听端口9000来处理它们。
最后的服务器块是你的默认。 它也监听端口80,并作为一个catchall – 任何不匹配的“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知道在文件系统上的确切位置。 此外,在更新的代码中, @backend块将重定向,并将nginx找不到匹配的所有请求传递给媒体wiki index.php文件进行处理。 这也使您可以使用干净的URL,如“wiki.example.com/Main_Page”(这将被重写为“wiki.example.com/index.php?title=Main_Page”)。