我在 DigitalOcean 上有一个 Ubuntu 服务器,它托管多个网站。我刚刚在我的 mac 上构建了一个 mean.js
堆栈应用程序,我计划将其部署到生产中,从而部署到这个现有服务器(尽管我不知道是否需要创建另一个像 here 这样的 Droplet) )。
我按照this link安装了node.js
和mongodb
等。然后,我从github克隆了自己的应用程序:
sudo git clone https://github.com/softtimur/myapp.git /opt/myapp
cd /opt/myapp
sudo npm install
npm start
因此,在浏览器中输入https://xxx.xx.xx.xx:3000/#/home
,即可与服务器良好通信。
现在,我想使用从 GoDaddy 购买的域名(即 myapp.io
)而不是 IP 地址与服务器通信。
我已修改 myapp.io
的 DNS 记录,使其指向 IP 地址。结果,https://www.myapp.io
很好地通向服务器,但是默认情况下它通向 nginx
设置的另一个页面。
然后,我将 /etc/nginx/sites-available/myapp.io
和 /etc/nginx/sites-enabled/myapp.io
设置如下:
server {
listen 3000;
listen [::]:3000;
root /opt/myopp/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name myopp.io;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
client_max_body_size 15M;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}
重新启动 nginx
后,npm start
返回错误:端口 3000 已在使用中
。
谁能告诉我这种方法是否正确?如果是,我该如何修复错误,例如 nginx 配置文件?
编辑1:在/etc/nginx/sites-avaiable/default
中,我有
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name xxx.xx.xx.x;
最佳答案
您想要做的是来自 //www.myapp.io
的反向代理至//xxx.xx.xx.xx:3000
。这是通过监听端口 80(或 443)并使用 proxy_pass
来实现的。连接到端口 3000 上运行的服务。请参阅 this document了解详情。
对于 http
服务器,您可以使用:
server {
listen 80;
server_name myopp.io;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
proxy_pass http://127.0.0.1:3000;
}
}
显然您正在使用https
这可以通过将您的服务更改为使用 http
来实现在端口 3000 上。使用 nginx
安装证书并终止 SSL 。请参阅this document了解更多。
关于node.js - 将没有端口号的域名链接到 nginx 托管的 MEAN stack 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43330776/