vue跨域2.x,3.x跨域,nginx跨域,nginx+域名+ssl证书配置

vue跨域2.x

文件位置

config---》index.js ---》大约13行---》

跨域内容

proxyTable:{
'/api': {
target: 'http://192.168.0.125:8000/info',//跨域地址
changeOrigin: true,
pathRewrite: {
'^/api': '',
}
}
},

使用

let baseUrl = '/api'
http.post(baseUrl + '/changepassword/',data)

vue跨域3.x

文件位置

根目录->vue.config.js

文件内容

module.exports = {
  devServer: {
    proxy: {
      '/HFweather': { //请求路径关键字
          target: 'https://devapi.qweather.com', //对应自己的接口
          changeOrigin: true,//是否允许跨域,在本地会创建一个虚拟服务端,然后发送请求的数据,
                              // 并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
          ws: true,
          pathRewrite: {
              '^/HFweather': ''      //这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替
                                  // 比如我要调用'http://localhost:8024/management/user/add',直接写‘/api/user/add’即可
          }
      }
    }
  },
}

使用

let baseUrl = '/api'
http.post(baseUrl + '/changepassword/',data)

nginx跨域

nginx全部配置在文章结尾

    server {

        #配置跨域问题 ↓   项目中的baseUrl 为let baseUrl = "/api"
        location /api/ {
            # 地址是假的,这个是后端的地址填自己的
            proxy_pass http://47.104.81.202:8002;
            # 这句话的意思是对路由进行重写,因为/api为代替后端地址的写在前端项目里,所以在访问的时候不加在后端路径会多加一个api,重写是为了去掉
            rewrite "^/api/(.*)" /$1  break;
        }
    }

域名和ssl证书配置

前提条件
有备案号域名,域名必须备案
申请的ssl证书,有免费和收费的
使用nginx配置的代理(我个人采用的)

我是用的域名和证书是在阿里云上购买的
再次强调一下域名不备案不能访问,备案信息可以在阿里云上操作,这里强调一点主题信息和域名申请的信息必须一致,不然不能通过备案

1、ssl证书
1.1、证书绑定域名:填写要绑定的域名
1.2、在域名控制台添加dnx解析地址:
复制:主机记录、记录值(如果证书和域名都在阿里云直接阿里会自动将域名的解析地址配置上了,直接点验证就通过了)
2、域名解析中:
2.1添加解析:
记录类型:选择txt
主机记录:复制的ssl证书的主机记录
记录值:复制的ssl证书的记录值
2.2、在添加一条解析:(一级域名)
记录类型:A-将域名指向一个ipv4地址
主机记录:www(例如:域名访问地址www.jd.com)
记录值:服务器地址,(线上项目文件所在服务器)
2.3、(选填)在添加一条解析:(二级域名)
记录类型:A-将域名指向一个ipv4地址
主机记录:erji(名字自己起合适的,erji.jd.com,二级域名访问要使用https得话也得在申请一个ssl证书然后加到这个域名解析中步骤2.1同)
记录值:服务器地址,(线上项目文件所在服务器)
3、nginx配置只说和配置https相关的
listen 必须是 443 ssl,
server_name 写域名
ssl证书的pem、key文件路径在ssl证书那里下载对应的,我选择下载的nginx的
listen 443 ssl;
server_name www.jd.com;

ssl证书的pem文件路径

ssl_certificate /root/ssl/8338221_crude.damolinks.com.pem;

ssl证书的key文件路径

ssl_certificate_key /root/ssl/8338221_crude.damolinks.com.key;
还有就是在完全没配好的情况下访问可能是显示不安全,重新开个网页访问就好了,再不行清一下浏览器缓存
证书域名什么直接也应该有个相应时间,可能配好稍微等一下才行,大概最长等五分钟左右,不行应该还是配置有错误

nginx 配置文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
     # 这里开始该---改的都有中文备注------------------------------------------------------------------------------

        listen       443 ssl;
        server_name deluxenergy.vip;
        #ssl on;#以前的版本用需要,现在废弃了
        #ssl证书的pem文件路径
        ssl_certificate  /root/ssl/8414610_deluxenergy.vip.pem;
        #ssl证书的key文件路径
        ssl_certificate_key /root/ssl/8414610_deluxenergy.vip.key;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
            #前端Vue-router 设置为history模式----↓这行必须加
            try_files $uri $uri/ /index.html;
        }

        #配置跨域问题 ↓   项目中的baseUrl 为let baseUrl = "/api"
        location /api/ {
            # 用uwsgin的时候才配这句话
            # include uwsgi_params;
            # 地址是假的,这个是后端的地址填自己的
            proxy_pass http://47.104.81.202:8002;
            # 这句话的意思是对路由进行重写,因为/api为代替后端地址的写在前端项目里,所以在访问的时候不加在后端路径会多加一个api,重写是为了去掉
            rewrite "^/api/(.*)" /$1  break;
            # 以下几句没搞懂
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header X-Real-IP $remote_addr;
        }
     # 更改到这里结束------------------------------------------------------------------------------
#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 html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1;  #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one  # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
posted @ 2022-11-16 09:35  嗯哼Nymph  阅读(233)  评论(0编辑  收藏  举报