【问题标题】:How to configure nginx configuration for rails on local machine?如何在本地机器上为 Rails 配置 nginx 配置?
【发布时间】:2020-07-24 15:44:01
【问题描述】:

通过端口 3000 上的 rails s 命令在 localhost:3000 上正常运行 rails 应用程序。 为了测试一些细节,想在我的 OSX 机器上的域名下运行它。 我在我的 Mac 上安装了 nginx,想知道如何配置它以使用 test.local 运行 localhost:3000

【问题讨论】:

标签: ruby-on-rails nginx


【解决方案1】:
  1. 从 brew 安装 nginx
brew install nginx
  1. 启动nginx服务
brew services start nginx
  1. 访问localhost:8080 以确保 nginx 正在运行。您应该会看到“欢迎使用 nginx”页面。

  2. 为 test.localhost 创建一个配置 brew install 的默认 nginx 配置目录是 /usr/local/etc/nginx/servers

cd /usr/local/etc/nginx/servers
nano test

并复制以下内容

server {
  listen 80;
  listen [::]:80;
  server_name test.localhost;
  root /<path to project>/public;
  access_log /<path to project>/log/nginx_access.log;
  error_log /<path to project>/log/nginx_error.log;
  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_set_header X-NginX-Proxy true;
    proxy_pass http://tupstream;
  }
}

upstream tupstream {
  server 127.0.0.1:3000;
}

  1. 现在我们需要使用 sudo 重新启动 nginx,因为我们将从 port 80 提供服务。
sudo brew services restart nginx
  1. 下一步是确保test.localhost 指向127.0.0.1 通过 brew 安装 dnsmasq
brew install dnsmaq
  1. 启动服务
sudo brew services start dnsmasq
  1. 复制 dnsmasq 配置文件
cp $(brew list dnsmasq | grep /dnsmasq.conf$) /usr/local/etc/dnsmasq.conf

## find the location of the file using
## brew list dnsmasq
## if the above doesn't work
  1. 确保 .localhost TLD 指向 127.0.0.1
echo 'address=/.localhost/127.0.0.1' >> /usr/local/etc/dnsmasq.conf
  1. 重启 dnsmasq
sudo brew services restart dnsmasq
  1. 编辑您的 DNS 服务器并确保 127.0.0.1 是第一个条目。 设置 -> 网络 -> WiFi -> 高级 -> DNS

确保您能够 ping .localhost 站点

ping test.localhost
PING test.localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.062 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.066 ms
^C
--- test.localhost ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.062/0.064/0.066/0.002 ms

由于 DNS 已更改,请确保您也可以访问远程站点 (ping google.com)

  1. 启动 RoR 服务器 @ 端口 3000
rails s -p 3000 -b 0.0.0.0
  1. 在浏览器中输入test.localhost

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2021-10-28
    • 2014-08-24
    • 2019-07-08
    相关资源
    最近更新 更多