我正在尝试在运行nginx thin的服务器上设置多个域.例如,我希望www.domain1.com和www.domain2.com转到不同的应用程序,这些应用程序具有到各自应用程序的不同根路径.
如果您熟悉nginx,我已在本文的底部发布了我的nginx.conf文件.
我以为我可以尝试使用多个服务器块,但是
然后我遇到了一个问题,服务器默认选择随机瘦端口,两个域都转到同一个应用程序. *主要原因是两个应用程序的所有端口都位于thin_cluster内部.*
我想我的主要担心是有一个与特定服务器无关的thin_cluster.然后是服务器块,它具有server_name等.但是,thin_cluster不能嵌套在服务器块内.
关于如何服务多个主机的任何想法?
这是我的/etc/nginx/nginx.conf文件
user nginx;
worker_processes 5;
error_log /var/log/nginx.error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx.access.log main;
sendfile on;
keepalive_timeout 65;
upstream thin_cluster {
server 0.0.0.0:3000;
server 0.0.0.0:3001;
server 0.0.0.0:3002;
server 0.0.0.0:3003;
server 0.0.0.0:3004;
}
server {
listen 80;
server_name www.domain1.com;
root /home/ec2-user/helloCloud/public;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://thin_cluster;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
解决方法:
您可以根据需要将其描述为mach“server”和“upstream”部分.
upstream cluster1 { ...; } upstream cluster2 { ...; } server { listen 80; server_name www.domain1.com; root /home/app1; location / { try_files $uri/index.html $uri.html $uri @backend; } location @backend { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://cluster1; } } server { listen 80; server_name www.domain2.com; root /home/app2; location / { try_files $uri/index.html $uri.html $uri @backend; } location @backend { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://cluster2; } }
这是一个例子.
我使用“try_files”而不是无意义的“if”块.只需在文档中阅读它.