华为云建站视频教程,东道设计官网,网站服务器多少钱,建筑模板使用寿命多久Nginx 中将前端请求中的所有以 “/apiUrl” 开头的路径代理到 http://192.12.200.101:9813 例如#xff1a; /apiUrl/login http://192.12.200.101:9813/login
配置nginx环境
进入Nginx 的配置文件编辑界面:
sudo nano /etc/nginx/conf.d/default.conf开始编辑 defaul…Nginx 中将前端请求中的所有以 “/apiUrl” 开头的路径代理到 http://192.12.200.101:9813 例如 /apiUrl/login http://192.12.200.101:9813/login
配置nginx环境
进入Nginx 的配置文件编辑界面:
sudo nano /etc/nginx/conf.d/default.conf开始编辑 default.conf
server {listen 80;server_name localhost;# 这里配置配置如下location /apiUrl/ {rewrite ^/apiUrl(/.*)$ $1 break; # 路径重写proxy_pass http://192.12.200.101:9813; # 代理请求转发的目标地址add_header Access-Control-Allow-Origin *; # 表示允许任何域的请求访问资源。add_header Access-Control-Allow-Methods *;# 定义允许跨域请求的 HTTP 方法proxy_set_header Content-Type application/x-www-form-urlencoded; charsetUTF-8; # 设置代理请求时的 HTTP 请求头部信息。}#charset koi8-r;access_log /var/log/nginx/host.access.log main;error_log /var/log/nginx/error.log error;location / {root /usr/share/nginx/html;index index.html index.htm;try_files $uri $uri/ /index.html;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location /50x.html {root /usr/share/nginx/html;}
}
纯净版这里复制
location /apiUrl/ {rewrite ^/apiUrl(/.*)$ $1 break;proxy_pass http://192.12.200.101:9813;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods *;proxy_set_header Content-Type application/x-www-form-urlencoded; charsetUTF-8;}注意 如果想/apiUrl/login http://192.12.200.101:9813/apiUrl/login 这里路径匹配规则 /apiUrl/如下图 /apiUrl ----匹配到的路径---- /apiUrl,/apiUrl1,/apiUrl2 /apiUrl/ ----匹配到的路径–只有 /apiUrl
允许多个信任域名进行跨域访问你可以在 Access-Control-Allow-Origin 头部中设置多个域名 多个域名时双引号或单引号内使用逗号隔开
add_header Access-Control-Allow-Origin https://your-allowed-domain-1.com, https://your-allowed-domain-2.com, https://your-allowed-domain-3.com;
自定义允许跨域请求的 HTTP 方法
add_header Access-Control-Allow-Methods GET, POST, OPTIONS, DELETE;nginx官方文档–rewrite