【发布时间】:2020-07-24 15:44:01
【问题描述】:
通过端口 3000 上的 rails s 命令在 localhost:3000 上正常运行 rails 应用程序。 为了测试一些细节,想在我的 OSX 机器上的域名下运行它。 我在我的 Mac 上安装了 nginx,想知道如何配置它以使用 test.local 运行 localhost:3000
【问题讨论】:
标签: ruby-on-rails nginx
通过端口 3000 上的 rails s 命令在 localhost:3000 上正常运行 rails 应用程序。 为了测试一些细节,想在我的 OSX 机器上的域名下运行它。 我在我的 Mac 上安装了 nginx,想知道如何配置它以使用 test.local 运行 localhost:3000
【问题讨论】:
标签: ruby-on-rails nginx
brew install nginx
brew services start nginx
访问localhost:8080 以确保 nginx 正在运行。您应该会看到“欢迎使用 nginx”页面。
为 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;
}
sudo 重新启动 nginx,因为我们将从 port 80 提供服务。sudo brew services restart nginx
test.localhost 指向127.0.0.1
通过 brew 安装 dnsmasqbrew install dnsmaq
sudo brew services start 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
echo 'address=/.localhost/127.0.0.1' >> /usr/local/etc/dnsmasq.conf
sudo brew services restart dnsmasq
确保您能够 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)
rails s -p 3000 -b 0.0.0.0
test.localhost
【讨论】: