Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
dev_server_proxyのススメ
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
hirasa
December 06, 2021
Technology
170
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
dev_server_proxyのススメ
dev_server_proxyのススメ
hirasa
December 06, 2021
More Decks by hirasa
See All by hirasa
redux使うのやめました
hirasa
1
620
eslintのプラグインを作成した話
hirasa
1
130
Other Decks in Technology
See All in Technology
2026-09-18 gotanda.sre Terraformで複数環境作ったり、複数Stateに分割したりそれとTerragrunt / Terraform multi envs and multi states
masasuzu
4
680
Azure Serverless 2026:Production-ready な AI エージェント基盤 / Azure Serverless 2026: Production-Ready AI Agent Platform
miyake
2
460
AgentCore Runtime上にAgentic Coding基盤を構築・展開する際の設計ポイントと限界点 / Design considerations and limitations when building an agentic coding platform on AgentCore Runtime
har1101
5
590
Deployment の 先にある AI Agent 基盤 - kagent vNext、Agent Substrate、Hermes から読み解く Agent Runtime の現在地 / k8s-matsuri-2-ai-agent-platform-amsy810
masayaaoyama
4
690
新機種発売前に見直そう!端末移行で再ログインが要るアプリ・要らないアプリは何が違うのか 〜シームレスに再開できる設計と実装〜
zozotech
PRO
0
220
「ピッケル本」日本語版は4.0(第6版)が出版されるべき / pickaxe4-nagoyark05
kakutani
2
260
安心して変更できるWebフロントエンドの作り方
pirosikick
5
2.8k
データ界隈LT祭 第1回LT登壇
taromatsui_cccmkhd
2
1.5k
登壇の自信を奪う3匹のオバケ / 3 Ghosts That Rob You of Your Confidence in Public Speaking
pauli
9
1k
アクセスキーこわい やめかたと漏らさない工夫
sassssan68
1
560
ユーザー価値を届け続けるためにウォンテッドリーが大切にしている文化
kotaminato
0
190
C#未経験の僕がAIに読めるコードを書かせるまで
maguroalternative
0
200
Featured
See All Featured
From π to Pie charts
rasagy
1
370
Unsuck your backbone
ammeep
672
58k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Why Our Code Smells
bkeepers
PRO
340
58k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.1k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Code Review Best Practice
trishagee
74
20k
A designer walks into a library…
pauljervisheath
211
25k
Transcript
Dev server – proxyのススメ 平崎 葵(ひらさき まもる) 2021/12/06
自己紹介 平崎 葵 (ひらさき まもる) クーガー株式会社 所属 https://couger.co.jp/ ◦フロントエンド、サーバサイドエンジニア ◦バーチャルヒューマンエージェントの開発
趣味:音楽、ドラム、作曲
発表内容 Dev serverのproxy機能を使用して、 効率よくcors対策をしよう。
Cors (Cross Origin Resource Sharing)とは? Web Server1 (https://web_server1.co.jp) Web Server2
(https://web_server2.co.jp) ユーザのブラウザ ②Jsやcss、html をダウンロード ①Web Server1の サイトが見たい ③画面が表示され、 jsが実行される。 ❌ ④jsで裏側でWeb Server2にアク セス使用とするとブロックされる。 Webセキュリティの重要なポリシーの一つ「Same-Origin Policy(同一オリジンポリシー)」に関わる。 参考URL ◦ https://javascript.keicode.com/newjs/what-is-cors.php (CORS とは?) ◦ https://qiita.com/att55/items/2154a8aad8bf1409db2b (なんとなく CORS がわかる...はもう終わりにする。)
Corsで防ぎたい脆弱性 lXSS (Cross Site Scripting) ユーザがWebサイトにアクセスすることで不正なスクリプ トがWebブラウザ上で実行されてしまう脆弱性。 lCSRF (Cross-Site Request
Forgeries) ユーザが、他サイトでの意図しない処理をブラウザ上で 実行される脆弱性。(ログインしたユーザにしか実行でき ない処理「記事の投稿や削除など」)
ローカル開発(localhost)でもcorsは有効 Front end dev server (http://localhost) Server side dev server
(http://localhost:8080) ローカルで $ npm run startなどで開発用サーバを実行 Mysqlやredis, elastic searchな どはdockerで立ち上げている。 ブラウザ ②Jsやcss、htmlをダ ウンロード ①front end開発 のためアクセス ❌ ③Web API実行 (例:/api/user/login) Corsでひっかかる。
Corsをgoogle chromeデベロッパーツールで確認 http://localhostから http://localhost:8080にアクセス しようとしたので「同一オリジンポ リシー」に反する。 Origin: http://localhost 見えてないですけど、アクセス元 のオリジン情報がリクエストヘッ
ダーに含まれている。
Corsはサーバ側で許可することで回避できる。 l Laravelの場合 https://qiita.com/kyo-san/items/a507aa0b46037df1b139 (LaravelでのCORS対策とmiddlewareへの理解) https://github.com/fruitcake/laravel-cors l Nodejs (express)の場合 https://expressjs.com/en/resources/middleware/cors.ht
ml l Python (flask)の場合 https://flask-cors.readthedocs.io/en/latest/
サーバ側設定例 // app.js const cors = require('cors') app.use( cors({ credentials:
true, origin: ['http://localhost'], optionsSuccessStatus: 200, }), ) Nodejs (express) Php (Laravel ) // config/cors.php … ‘allowed_origins’ => [’http://localhost*’] … ※Laravelのバージョンで導入方法が違う 可能性あり https://github.com/fruitcake/laravel-cors Python (Flask) # $ pip install -U flask-cors app = Flask(__name__) cors = CORS(app, resources={r"/api/*": {"origins": ”http://localhost"}}) Access-Control-Allow-Origin: http://localhost Access-Control-Allow-Headers "X-Requested-With, Origin, X-Csrftoken, Content-Type, Accept" 設定すると、サーバからのResponseのheaderに以下のような情報が追加される。
サーバサイドにcors対策を入れて解決はできた。 しかし、フロントのdev serverのproxyの機能を使えば、 サーバサイドに手を入れることなくcors対策ができる。
Dev Server Proxyを導入する。 Front end dev server (http://localhost) Server side
dev server (http://localhost:8080) Mysqlやredis, elastic searchな どはdockerで立ち上げている。 ブラウザ ②Jsやcss、htmlを ダウンロード ①front end開発 のためアクセス ◦ ③Web API実行 (例:/api/user/login) Dev serverのproxy経由でアクセス。 ④proxyがserver sideのサーバにアクセス リクエストヘッダーの「Origin」ヘッダーをproxy サーバで変換して、corsの辻褄合わせをしてい る。
dev server proxy設定方法① ▪web packの場合 // webpack.config.js … devServer: {
static: { directory: path.join(__dirname, '__public/'), }, compress: true, historyApiFallback: { index: "", disableDotRule: true }, proxy: { "/api/**": { target: 'http://localhost:8080', withCredentials: true, secure : false, } } }, ※/apiから始まるURLをproxy経由にする場合。 https://webpack.js.org/configuration/dev-server/
dev server proxy設定方法② ▪Vue-cliの場合 // vue.config.js devServer: { proxy: {
"^/api": { target: "http://localhost:8080", changeOrigin: true, }, }, }, ※/apiから始まるURLをproxy経由にする場合。 https://cli.vuejs.org/config/#devserver-proxy
dev server proxy設定方法③ ▪React-create-appの場合 ※/apiから始まるURLをproxy経由にする場合。 https://create-react-app.dev/docs/proxying-api-requests-in-development/ // $ npm install
--save-dev http-proxy-middleware // src/setupProxy.js const createProxyMiddleware = require('http-proxy-middleware’) module.exports = (app) => { app.use( '/api', createProxyMiddleware({ target: 'http://localhost:8080', changeOrigin: true, withCredentials: true, secure: false, }), ) }
(余談)Localhostでhttpsしたい 1. ngrok ◦ローカルPC上で稼働しているサービスを外部公開できるサー ビス。 https://qiita.com/mininobu/items/b45dbc70faedf30f484e (ngrokが便利すぎる) 2. WebpackDevServerでhttps(TLS)ができるみたい(未検証) ◦https://blog.hinaloe.net/2017/04/14/tls-webpack-dev-server/
エンジニア募集中 https://couger.co.jp/news/career/ カジュアル面談実施中! lフロントエンドエンジニア lDevOpsエンジニア lAI/機械学習エンジニア lサーバアプリケーションエンジニア