我是 NGINX 新手,我试图将两个服务放入同一个 Docker 容器中。每个服务都有自己的域。
我发现这个答案这解决了我的问题,但还有另一个问题我无法解决。
如果我使用default.conf
下面的方法,两种服务都可以正常工作:
server {
listen 80;
listen [::]:80;
#server_name domain.me www.domain.me;
server_name 127.0.0.1; # FOR STAGING ONLY
error_log /var/log/nginx/error.log warn;
access_log /var/log/nginx/access.log combined;
location / {
proxy_pass http://website:8301; # The port must be the same of PROJECT_PORT
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# max uploadable file size
client_max_body_size 4G;
}
location /static/ {
# static folder on the server
alias /app/web/static-folder/;
}
location /media/ {
# media folder on the server
alias /app/web/media-folder/;
}
}
server {
#server_name geoserver.domain.me www.geoserver.domain.me;
server_name 127.0.0.2; # FOR STAGING ONLY
listen 80;
listen [::]:80;
server_tokens off;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types
text/plain
text/css
application/json
application/javascript
application/x-javascript
text/xml application/xm
application/xml+rss
text/javascript;
underscores_in_headers on;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_buffer_size 1k;
proxy_buffers 24 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 2048m;
proxy_temp_file_write_size 32k;
location / {
deny all;
return 301 /geoserver;
}
location /geoserver {
proxy_pass http://geoserver:8080/geoserver;
}
}
相反,如果我使用下面的代码,域1有效,但是域2给500 Bad Gateway
:
upstream domain1 {
server website:8301; # same container's name of docker-compose.yml
# NB: The port must be the same of PROJECT_PORT
}
upstream domain2 {
server geoserver:80; # same container's name of docker-compose.yml
}
server {
listen 80;
listen [::]:80;
server_name 127.0.0.1; # FOR STAGING ONLY
error_log /var/log/nginx/error.log warn;
access_log /var/log/nginx/access.log combined;
location / {
proxy_pass http://domain1/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# max uploadable file size
client_max_body_size 4G;
}
location /static/ {
# static folder on the server
alias /app/web/static-folder/;
}
location /media/ {
# media folder on the server
alias /app/web/media-folder/;
}
}
server {
server_name 127.0.0.2; # FOR STAGING ONLY
listen 80;
listen [::]:80;
server_tokens off;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types
text/plain
text/css
application/json
application/javascript
application/x-javascript
text/xml application/xm
application/xml+rss
text/javascript;
underscores_in_headers on;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_buffer_size 1k;
proxy_buffers 24 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 2048m;
proxy_temp_file_write_size 32k;
location / {
deny all;
return 301 /geoserver;
}
location /geoserver {
proxy_pass http://domain2/geoserver/;
}
}
为什么会发生这样的事?