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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
Railsのように考える: See through the Master
snoozer05
PRO
3
840
安心して変更できるWebフロントエンドの作り方
pirosikick
4
2.5k
SREは、MCPとAutopilotをこう使え!
kazumax55
2
800
Screen Lens - 今見てる画面を翻訳する
komagata
0
290
幾何アルゴリズムで なめらかなピン操作を / iOSDC Japan 2026 / smoothpin
kazumanagano
0
350
現場で役立つ技術負債の効果的な返済方法
masuda220
PRO
8
4k
【技術的負債conf】事業成長に伴う技術的負債の説明責任とAIによるモニタリング、認知的負債について
i35_267
2
1.6k
作品が生態系になった ─ Mini Tokyo 3D から世界へ
nagix
0
190
顧客に向き合う開発組織へ。リアーキテクチャとフィーチャーチーム化で挑む組織改革
safie
0
1.9k
What the customer really needed
kawaguti
PRO
2
170
技術的負債から考える、AI時代のエンジニアリング投資 — ビズリーチの技術的負債と向き合った経験から、変更し続けられるソフトウェアを考える/ technical-debt-con2026
visional_engineering_and_design
3
2.9k
Minecraft JavaのMODをSwiftで作る
1mash0
0
160
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
1
470
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
360
Game over? The fight for quality and originality in the time of robots
wayneb77
1
280
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.2k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
670
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
540
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
230
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
910
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
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サーバアプリケーションエンジニア