我有一台装有 Ubuntu 的服务器 (VPS Amazon)。在这个服务器上运行我的后端节点和我的前端 React。 我的 ReactApp 在 nginx 上运行,我的后端在 pm2 上运行。
在我的 React 应用程序中,我定义了 REACT_APP_BASE_URL:http://[my_ip_server]:4000。 所以一切正常,但在 nginx 中配置 SSL 后,我可以访问我的前端登录页面,但是当我发送请求时,我发现以下错误:
a) 如果我在 REACT_APP_BASE_URL (https://[my_ip_server]:4000) 中设置了 https,则会收到此错误:ERR_SSL_PROTOCOL_ERROR。 b) 如果我让 http 出现混合内容错误
有人知道我是怎么做这项工作的吗?
非常感谢!
我的 nginx.conf。目前我只使用端口 80,直到我解决我的问题。
server {
#listen [::]:443 ssl ipv6only=on; # managed by Certbot
#listen 443 ssl; # managed by Certbot
#ssl_certificate /etc/letsencrypt/live/mysite.com.br/fullchain.pem; # managed by Certbot
#ssl_certificate_key /etc/letsencrypt/live/mysite.com.br/privkey.pem; # managed by Certbot
#include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
#ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
#if ($host = surveys.alcancenet.com.br) {
# return 301 https://$host$request_uri;
#} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name mysite.com.br;
#return 404; # managed by Certbot
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri /index.html;
}
}
答案 0 :(得分:1)
在@Markido 的帮助下。我设法解决了这个问题。
我在后端添加了默认路由“/api”,然后在我的 nginx 配置中添加了以下内容
POST
谢谢!!!
答案 1 :(得分:0)
首先,运行应用程序(这是我假设您使用 PS2 的目的)和通过 nginx 代理公开它们之间存在差异。
最好向我们展示您的 nginx 配置文件,并告诉我们您的后端在哪个端口上运行(假设前端在端口 4000 上运行)。
编辑; 感谢您提供配置和后端端口。
我认为您不需要将 create react app base url 设置为 https,只需设置端口并使用 PS2 在 VPS 上运行即可。
我看不到您的配置中有任何指向 4000 的代理 - 您没有公开后端吗?
唯一暴露的部分是静态 html 文件。相关代码为;
from multiprocessing import Pool
from functools import partial
class klass:
def __init__(self, a):
self.value = a
import os
self.module = os #this fails: can't pickle a module and send it back to main process
def foo(a, b, c):
return klass(a+b+c)
if __name__ == "__main__":
with Pool() as p:
a = 1
b = 2
bar = partial(foo, a, b)
res = p.map(bar, range(10))
print([r.value for r in res])
如果您想使用 https 调用后端,或使用某些工具生成您的站点,并且需要 HTTPS 调用的过程,您需要在前端正确执行此操作。 IE 中的某些内容不在这里。
通常的做法是; 将后端和前端只暴露在SSL的443端口,使用不同的子域(例如api.mydomain.com),然后使用nginx中的代理将每个域的443流量重定向到对应的本地端口(4000,以及前端端口或静态文件目录更有可能)。
代替:
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri /index.html;
}
使用类似:
location / {
try_files $uri $uri /index.html;
}