vue.js で Nginx を使用してクロスドメインの問題を解決する

亚连
リリース: 2018-06-07 18:17:15
オリジナル
3331 人が閲覧しました

ここで、クロスドメインの問題を解決するための vue.js プロキシの使用と Nginx の使用に関する記事を共有します。これは非常に参考になるので、皆さんのお役に立てれば幸いです。

クロスドメインの問題を解決するには、Nginx リバース プロキシを使用します (vue.js はプロキシを使用して、クロスドメインが原因で vue.js によってトリガーされたオプション リクエストを削除します)

私たちのプロジェクトでは、依然としてコンテナとして Node.js が必要です

1. Windows に Nginx をインストールします (公式 Web サイト http://nginx.org/en/download.html から安定版をダウンロードします)

2. nginx のサーバー

server { 
listen 8899;// 你的端口 
server_name localhost; 
root C:/ZOBSF_F/dist;//你打包部署的文件路径 
#charset koi8-r; 
#access_log logs/host.access.log main; 
# 匹配 api 路由的反向代理到API服务 
location ^~/api { 
proxy_pass http://119.23.227.141:10001/;//你的后端IP和端口 
} 
#根据路由设置,避免出现404 
location / { 
try_files $uri $uri/ @router; 
index index.html; 
} 
location @router { 
rewrite ^.*$ /index.html last; 
} 
#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; 
#} 
}
ログイン後にコピー

を変更します。 conf ファイルを設定に追加します。 3. uve.js によってパッケージ化された dist ファイルは次のとおりです

4. 次に、プロジェクトの起動設定ファイルserver.production.js

var express = require('express');
var app = express();
var compression = require('compression');
var proxyMiddleware = require('http-proxy-middleware')
var history = require('connect-history-api-fallback');
app.use(compression());
app.use(express.static(__dirname));
app.middleware = [//使用代理api
    proxyMiddleware(['/api'], {target: 'http://192.168.11.103:10001',
    changeOrigin: true,
    pathRewrite: {
    '^/api' : '/', 
  },}),
];
app.get('*', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});
app.use(history());
app.use(app.middleware);

app.listen('8080', function(error) {
  console.info("==================系统正在启动中...=============================");
 if (error) {
  console.error(error)
 } else {
  console.info("==================9999系统启动成功!!!=============================")
 }
});
ログイン後にコピー

5を追加します。次に、プロジェクト ディレクトリでコマンド ノードを使用します。server.production.js が Enter キーを押すと、パッケージに依存関係がないことがわかります。npm install [エラー メッセージの依存関係コンポーネント] を使用して、express、compression、http-proxy-middleware、などそれぞれ。

次に、プロジェクトを開始し、アドレス http://localhost:8080/xxx にアクセスします

6 番目に、vue.js は Baidu によってプロキシとして使用できます (構成設定ファイル内の inde.js を変更するだけです)。 )

 proxyTable: {
     '/api': {
    target: Host.Host,//设置你调用的接口域名和端口号 别忘了加http
    changeOrigin: true,
    pathRewrite: {
     '^/api': '/'//这里理解成用‘/api'代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add'即可
    }
   }
  },
ログイン後にコピー

以上は皆さんのためにまとめたもので、今後皆さんのお役に立てれば幸いです。

関連記事:

vue+webpack で非同期コンポーネント読み込みを実装するには?

vue-resource の jsonp クロスドメインの問題について

vue の親コンポーネントを通じてサブコンポーネントのスタイルを変更する方法

以上がvue.js で Nginx を使用してクロスドメインの問題を解決するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!