我为应用程序提供了两个Nginx配置:
1)Rails应用
upstream app {
server unix:/home/deploy/railsapp/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name api.example.com;
root /home/deploy/railsapp/current/public;
try_files $uri/index.html $uri @app;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection '';
proxy_pass http://app;
}
location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
2)静态html / js应用程序
server {
listen 81;
server_name client.example.com;
root /home/deploy/clientapp;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection '';
# proxy_pass http://app;
}
location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
gzip_static on;
expires max;
add_header Cache-Control public;
}
}
问题是这两个地址api.example.com
并api.example.com
打开相同的应用程序(api.example.com
)
一些关于nginx config的基础教程;https://www.digitalocean.com/community/tutorials/how-to-configure-the-nginx-web-server-on-a-virtual-private-server。您应该对Rails应用程序使用代理,而不对静态html应用程序使用代理。
server {
listen 80; #should also be 80
server_name client.example.com;
root /home/deploy/clientapp;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
gzip_static on;
expires max;
add_header Cache-Control public;
}
}
要测试您可以执行的配置,是否nginx -t
应该nginx -s reload
使用新配置重新加载?如前所述,此问题与rails无关,它只是nginx配置。
会根据server_name
请求返回哪个网站,如果没有default_server
,它将返回加载到nginx配置中的最后一个网站。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句