问题描述
在本地用create-react-app起一个服务(http://localhost:3000),请求服务器接口,报如下错误:
产生原因
浏览器不允许跨域请求
解决方案
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080; // nginx监听的端口号
server_name localhost;
location / { // 通用匹配,任何请求都会匹配到
proxy_pass http:/ /localhost:3000; // 本地起的node服务
}
location /equip-run-manager {
proxy_pass http:/ /192.168.100.250:9999; // 接口的请求地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/ *;
}
访问方式
从原来的http://localhost:3000 -> http://localhost:8080,匹配nginx的8080端口
location后面的斜杠
参见文档://www.greatytc.com/p/c751250a5112
nginx日志文件(排错)
nginx.conf文件日志文件的默认存放位置
/usr/local/var/log/nginx
参数说明
log_format main '$remote_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'$http_user_agent $http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status';
access_log /usr/local/var/log/nginx/access.log main;
127.0.0.1 - [26/Jan/2021:11:48:12 +0800] "POST /equip-run-manager/Spring/MVC/entrance/unifier/QueryAlarmDictData HTTP/1.1" 200 135 "http://localhost:8080/alarm" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 - 0.543 0.543 192.168.100.250:9999 200
$remote_addr 客户端地址(127.0.0.1)
$remote_user 客户端用户名称 (-)
$time_local 访问时间和时区(26/Jan/2021:12:00:20 +0800)
$request 请求的URI和HTTP协议(POST /equip-run-manager/Spring/MVC/entrance/unifier/QueryAlarmDictData HTTP/1.1)
$status HTTP请求状态 (200)
$body_bytes_sent 发送给客户端文件内容大小(135)
$http_referer 浏览器访问的页面地址 (http:/ /localhost:8080/alarm)
$http_user_agent 用户终端浏览器等信息 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36)
$request_time 整个请求的总时间 (0.543)
$upstream_response_time 请求过程中,upstream响应时间 (0.543)
$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 (192.168.100.250:9999)
$upstream_status upstream状态 (200)