将 Nginx 与 Create-React-App 开发服务器和 npm start 一起使用
Using Nginx with Create-React-App dev server and npm start
这里的问题令人沮丧。我在必须使用 Nginx 作为外部 connection/HTML 服务器的服务器上使用 created-react-app 创建了一个反应项目。本质上我需要做的是使用 nginx 作为代理服务器,当 URL 被输入浏览器时,请求被路由到 localhost:3000 并且反应应用程序生成结果由nginx.
到目前为止,这是 nginx 文件,但我无法让它工作。问题数量:
root指定是否需要指向react项目中的/public目录才能获取index.html?
假设您 运行 npm start
并且在编译后,您在浏览器中输入 FQDN,它会路由到 localhost:3000
到目前为止我的 Nginx 配置
server_name server-name.com;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/site-name/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site-name/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
proxy_read_timeout 60s;
client_max_body_size 64M;
# should this be routed to /public in the react project to pick up index.html?
# root /var/www/html;
index index.html index.htm;
location / {
#try_files $uri $uri/ =404;
# Make sure React Application return is index.html so client routing remains in sync/reachable
#try_files $uri /index.html;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /_api {
rewrite ^/_api/(.+) / break;
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
}
server {
server_name server-name.com
listen 80;
allow 10.0.0.0/16;
deny all;
proxy_read_timeout 60s;
client_max_body_size 64M;
# Should this be routed to /public in the react project to pick up index.html?
# root /var/www/html;
index index.html index.htm;
location / {
#try_files $uri $uri/ =404;
# Make sure React Application return is index.html so client routing remains in sync/reachable
try_files $uri /index.html;
}
location /_api {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
}
打开你的 nginx 文件 vi sites-enabled/default 如果你不知道该怎么做,请告诉我。
然后从该文件中删除所有内容并简单地粘贴:
server {
listen 80;
client_max_body_size 10M;
server_name YourWebsiteNameWithDomain yourIPV4;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
别忘了重启你的 nginx
这里的问题令人沮丧。我在必须使用 Nginx 作为外部 connection/HTML 服务器的服务器上使用 created-react-app 创建了一个反应项目。本质上我需要做的是使用 nginx 作为代理服务器,当 URL 被输入浏览器时,请求被路由到 localhost:3000 并且反应应用程序生成结果由nginx.
到目前为止,这是 nginx 文件,但我无法让它工作。问题数量:
root指定是否需要指向react项目中的/public目录才能获取index.html?
假设您 运行
npm start
并且在编译后,您在浏览器中输入 FQDN,它会路由到 localhost:3000
到目前为止我的 Nginx 配置
server_name server-name.com;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/site-name/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site-name/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
proxy_read_timeout 60s;
client_max_body_size 64M;
# should this be routed to /public in the react project to pick up index.html?
# root /var/www/html;
index index.html index.htm;
location / {
#try_files $uri $uri/ =404;
# Make sure React Application return is index.html so client routing remains in sync/reachable
#try_files $uri /index.html;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /_api {
rewrite ^/_api/(.+) / break;
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
}
server {
server_name server-name.com
listen 80;
allow 10.0.0.0/16;
deny all;
proxy_read_timeout 60s;
client_max_body_size 64M;
# Should this be routed to /public in the react project to pick up index.html?
# root /var/www/html;
index index.html index.htm;
location / {
#try_files $uri $uri/ =404;
# Make sure React Application return is index.html so client routing remains in sync/reachable
try_files $uri /index.html;
}
location /_api {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3030;
}
}
打开你的 nginx 文件 vi sites-enabled/default 如果你不知道该怎么做,请告诉我。
然后从该文件中删除所有内容并简单地粘贴:
server {
listen 80;
client_max_body_size 10M;
server_name YourWebsiteNameWithDomain yourIPV4;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
别忘了重启你的 nginx