gpt4 book ai didi

ruby-on-rails - 如何添加 www.使用 NginX 和 SSL 域名?

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:09 25 4
gpt4 key购买 nike

我有一个带有 Rails 4 应用程序的 VPS,在带有 NginX 和 Unicorn 的 Ubuntu 上运行。

因为我希望所有页面都受 SSL 保护,所有对 http:// 的请求都被转发到工作正常的 https://

这是我的 NginX 配置:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;

server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

gzip on;
gzip_disable "msie6";
gzip_types text/plain text/xml text/css text/comma-separated-values;
upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

server {
listen 80;
server_name myapp.com;
rewrite ^ https://$server_name$request_uri? permanent;
}

server {
listen 443;
server_name myapp.com;
root /home/rails/public;
index index.htm index.html;

ssl on;
ssl_certificate /etc/ssl/myapp.com.crt;
ssl_certificate_key /etc/ssl/myapp.com.key;

location / {
try_files $uri/index.html $uri.html $uri @app;
}

location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://app_server;
}
}
}

我怎样才能将对 http://myapp.comhttps://myapp.com 的所有请求都转发到 https://www.myapp.com?

感谢您的帮助。

最佳答案

您可以通过两种方式进行:

  • 在您的 Rails 应用程序中,检测请求的 URI 是否包含 www 并基于此提供 30(1|2) 重定向(可能是最简单的)。
  • 在 Nginx 中创建一个额外的虚拟主机,监听 myapp.com 并且只让它重定向到 www.myapp.com

    服务器{ 听 443; 服务器名称 myapp.com; [添加 ssl 配置] 返回 301 https://www.myapp.com $request_uri;

关于ruby-on-rails - 如何添加 www.使用 NginX 和 SSL 域名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24148126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com