← Back to Apache Course | Chapter 3: Modules | Lesson 3 of 5

mod_proxy का उपयोग

mod_proxy Apache को requests को किसी दूसरे server पर forward करने देता है -- आमतौर पर Gunicorn, Node.js, या किसी दूसरी machine पर एक backend जैसा एक application server -- और response को वापस ऐसे handle करता है जैसे Apache ने खुद इसे generate किया हो।

Forward बनाम Reverse Proxy

एक forward proxy clients के आगे बैठता है, उनकी ओर से internet fetch करता है (जैसे एक corporate proxy)। एक reverse proxy servers के आगे बैठता है, public internet से requests लेकर उन्हें किसी backend तक forward करता है जिससे public कभी सीधे बात नहीं करती। Apache के साथ mod_proxy लगभग हमेशा दूसरे तरीके से इस्तेमाल होता है, जिसे इस course में बाद के dedicated Reverse Proxy lesson में पूरी तरह cover किया गया है।

सही Sub-Modules Enable करना

mod_proxy खुद base module है, लेकिन आपको एक protocol-specific module भी चाहिए: HTTP/HTTPS backends के लिए mod_proxy_http, या WebSocket connections के लिए mod_proxy_wstunnel। Ubuntu पर: sudo a2enmod proxy proxy_http (ज़रूरत पड़ने पर proxy_wstunnel भी add करें)।

ProxyPass और ProxyPassReverse

ProxyPass "/api/" "http://127.0.0.1:8000/" /api/ के नीचे किसी भी request को उस backend address पर forward करता है। ProxyPassReverse उल्टी direction में matching काम करता है: यह backend से वापस आने वाले Location/Set-Cookie headers को rewrite करता है ताकि वे internal की जगह public-facing URL reference करें, जो redirects और cookies के सही काम करने के लिए मायने रखता है।

एक Typical Use Case

एक बहुत common pattern: एक Python (Gunicorn/uWSGI) या Node.js app चलाना जो सिर्फ 127.0.0.1:8000 पर सुने, internet को बिल्कुल भी exposed न हो, और port 80/443 पर Apache को अकेली चीज़ बनने दें जिससे public actually बात करे -- TLS, static files, और logging handle करते हुए, फिर dynamic requests को app तक proxy करते हुए।

Note: same path के लिए हमेशा ProxyPass को ProxyPassReverse के साथ pair करें -- अकेले ProxyPass request को ठीक forward कर देता है, लेकिन backend द्वारा भेजा कोई भी redirect गलत (internal) address पर pointing छोड़ देता है।

उदाहरण: Proxying /api/ to a local backend

apacheconf
ProxyPreserveHost On
ProxyPass "/api/" "http://127.0.0.1:8000/"
ProxyPassReverse "/api/" "http://127.0.0.1:8000/"
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. mod_proxy enable करना लेकिन mod_proxy_http भूल जाना, ताकि plain HTTP/HTTPS proxying actually काम ही न करे।
  2. matching ProxyPassReverse के बिना ProxyPass इस्तेमाल करना, backend से वापस आने वाले redirects या cookies को तोड़ते हुए।
  3. Apache से proxy करने के अलावा backend app server के port को सीधे internet पर expose करना, जिससे Apache को आगे रखने का पूरा मतलब ही खत्म हो जाता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.