首页 > 编程知识 正文

nginx跨域解决方案,跨域请求的解决方案

时间:2023-05-04 14:54:47 阅读:131314 作者:3916

文章简介修改hosts文件velocityerp.top.conf配置文件nginx配置header参数(域间解析)、接口端配置nginx反向代理(域间解析)、发送端配置nginx端口

前言介绍了LNMP环境下的nginx配置,详细介绍了域名访问和nginx配置问题,有关在linux环境下安装nginx的信息,请参考centos7构建php7.1.3 mysql5.7 nginx1.11

修改主机文件以设置要访问的本地域名称。 例如,velocityerp.top

主机文件vi /etc/hosts# 添加127.0.0.1 velocity ERP.top 127.0.0.1 localhost localhost.local domain localhost4. local domain 43360336000,如下.0.0. 1在velocity ERP.top中添加/usr/local/nginx/conf配置文件

服务器{ listen 80; #监听端口server_name velocityerp.top; #站点域名root '/mnt/hgfs /项目/www/ERP/public '; #代码地址location/{ index index.PHP index.html error/index.html; if (! -e$request_filename}{#如果找不到请求的路径,nginx是否要执行以下重写rewrite^(.* ) $ /index.php? s=/$1 last; 访问www.baidu.com/test.html,如果找不到,跳至www.Baidu.com/index.PHP/test.html,设置访问路径,然后单击index.phpbreak }location~.php(.* ) $ )是以. PHP结尾的所有请求和区分大小写的fastcgi_pass 127.0.0.1:9000; #本地9000端口侦听php-fpmfastcgi_index index.php; #如果请求url以“/”结尾,请稍后添加index.php。 此处的设置实际上无效。 我以为以前以. php结尾的请求受到限制,所以习惯使用fastcgi_split_path_info ^ ()。 u ().PHP ) ) ) /?) $; #非贪婪是fastcgi _ param script _ filename $ document _ root $ fastcgi _ script _ name; 在#浏览器中访问的. php文件实际上正在读取$document_root (站点根目录)下的. php文件。 也就是说,访问127.0.0.1/index.php文件时,必须阅读站点根目录下的index.php文件fastcgi脚本请求的值fastcgi _ param path _ I 解析include fastcgi_params; 重新启动nginx

service nginx restart只需要本地访问velocityerp.top

nginx构成header参数(跨域解析),接口端为server { listen 80; server _ name API.velocity public.top; root '/mnt/hgfs /项目/www/velocity public-API/public '; add _ header ' access-control-allow-origin ' * '; #允许的请求源,*为所有add _ header ' access-control-allow-credentials ' ' true '; #是否允许浏览器继续请求携带凭据(Cookies )。 此值仅为true。 如果服务器不从浏览器发送cookie,请删除此字段。 add _ header ' access-control-allow-methods ' ' get,POST,OPTIONS '; #的值是逗号分隔的字符串,指示服务支持的所有域间请求的方式。 请注意,不仅会返回浏览器请求的方法,还会返回所有支持的方法。 这不要求add _ header ' access-control-allow-headers ' ' authorization,Content-Type,Accept,Origin,user-多次

Mx-ReqToken,Keep-Alive,X-Requested-With,Pragma,Cache-Control,If-Modified-Since,token'; #浏览器请求包括Access-Control-Request-Headers字段,则Access-Control-Allow-Headers字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段,不限于浏览器在"预检"中请求的字段。 add_header 'Access-Control-Max-Age' 1728000; 该字段可选,用来指定本次预检请求的有效期,单位为秒。上面结果中,有效期是20天(1728000秒),即允许缓存该条回应1728000秒(即20天),在此期间,不用发出另一条预检请求 location / { index index.php index.html error/index.html; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; } } location ~ .php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; }} nginx反向代理(解决跨域),发送方配置 server {listen 80;server_name tp5.top;root "/mnt/hgfs/project/www/tp5/public";location / {index index.php index.html error/index.html;if (!-e $request_filename) {rewrite ^(.*)$ /index.php?s=/$1 last;break;}}location ~ .php(.*)$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_split_path_info ^((?U).+.php)(/?.+)$;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;include fastcgi_params;}location /api { proxy_pass http://api.velocitypublic.top/index.php/api/auth/sliding_code; # 解决跨域,访问tp5.top/api 会代理到api.velocitypublic.top域名下,绕过浏览器同源策略检测 }} nginx端口转发(解决跨域) server {listen 80;server_name tp5.top;root "/mnt/hgfs/project/www/tp5/public";location / {index index.php index.html error/index.html;if (!-e $request_filename) {rewrite ^(.*)$ /index.php?s=/$1 last;break;}}location ~ .php(.*)$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_split_path_info ^((?U).+.php)(/?.+)$;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;include fastcgi_params;} location /api/ { # 把 /api 路径下的请求转发给真正的后端服务器 proxy_pass http://localhost:189/; # 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是localhost:189 proxy_set_header Host $http_host; # 把cookie中的path部分从/api替换成/service proxy_cookie_path /api /; # 把cookie的path部分从localhost:189替换成your.domain.name proxy_cookie_domain localhost:189 rampow.top; # 获取用户真实ID proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } nginx内部跳转 server {listen 80;server_name tp5.top;root "/mnt/hgfs/project/www/tp5/public";location / {index index.php index.html error/index.html;if (!-e $request_filename) {rewrite ^(.*)$ /index.php?s=/$1 last;break;}}location ~ .php(.*)$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_split_path_info ^((?U).+.php)(/?.+)$;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;include fastcgi_params;}location ^~ /api { alias /mnt/hgfs/project/www/wms_api/public; #目录别名,root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。root的处理结果是:root路径+location路径alias的处理结果是:使用alias路径替换location路径 if (!-e $request_filename) { rewrite ^ /api/index.php last; } }} nginx配置优先级详解 匹配优先级:精确匹配 >(^~) > 正则匹配 > 字符串(长 > 短)= 精确匹配;^~ 提高前缀字符串的匹配优先级;~ 区分大小写的正则表达式匹配;~* 不区分大小写的正则表达式匹配;/ 通用匹配(因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求),如果没有其它匹配,任何请求都会匹配到。# 举例location = /test.png { # 精确匹配 [ configuration A ] }location / { # 通用匹配,但是正则表达式和最长字符串会优先被匹配 [ configuration B ] }location /documents/ { # 前缀字符串匹配 # 匹配任何以 /documents/ 开头的请求 # 只有后面的正则表达式没有匹配到时,该配置才会被采用 [ configuration C ] }location ^~ /images/ { # 前缀字符串匹配 # 匹配任何以 /images/ 开头的请求,匹配成功以后,会停止搜索后面的正则表达式匹配 [ configuration D ] }location ~* .(gif|jpg|jpeg)$ { # 正则表达式匹配,匹配所有以 gif,jpg,jpeg 结尾的请求 # 然而,所有请求 /images/ 下的图片会被 configuration D 处理,因为 ^~ 指令,匹配不到这一条规则 [ configuration E ] }location /images/abc/ { # 前缀字符串匹配 # 只有去掉 configuration D 才能被匹配到 [ configuration F ] }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。