Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
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
160
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
600
eslintのプラグインを作成した話
hirasa
1
130
Other Decks in Technology
See All in Technology
マルチアカウント環境での コーディングエージェントを使った障害調査が大変なので AIエージェントにReadOnly権限を付与してみた / ReadOnly AI Agents for Multi-Account AWS Incident Response
yamaguchitk333
2
120
ザ・データベース、MySQL ~ OSC 2026 Sendai ~
sakaik
0
160
データレイクの「見えない問題」を可視化する
sansantech
PRO
1
140
AIチャット検索改善の3週間
kworkdev
PRO
2
150
Claude Codeをどのように キャッチアップしているか
oikon48
13
8.7k
[チョークトーク資料]AWS DevOps Agent を使いこなす / AWS Dev Ops Agent Chalk Talk AWS Summit Japan 2026
kinunori
3
680
AIネイティブな開発のサプライチェーンリスク対策 〜激動の開発現場でリスクに立ち向かう〜【ZennFes】
cscengineer
PRO
2
140
Flow 不死:AI 時代 DevOps 的不變本質
cheng_wei_chen
2
390
アンオフィシャルな、オフィシャルからのお願い
wyamazak_devrel
0
140
「ビジネスがわかるエンジニア」とは何か?
ryooob
0
130
小さく始める AI 活用推進 ― 日経電子版 Web チームの事例/nikkei-tech-talk47
nikkei_engineer_recruiting
0
310
現地で盛り上がった WWDC26 Keynote
zozotech
PRO
1
270
Featured
See All Featured
Claude Code のすすめ
schroneko
67
230k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
Mobile First: as difficult as doing things right
swwweet
225
10k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
400
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
Mind Mapping
helmedeiros
PRO
1
260
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
260
Designing for Performance
lara
611
70k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.3k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
170
Navigating Team Friction
lara
192
16k
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サーバアプリケーションエンジニア