إعداد بروكسي باستخدام Nginx

الجميع يعرف Nginx كخادم ويب وموازن أحمال (Load Balancer) وخادم وكيل عكسي (Reverse Proxy) شهير. لكن، هل تعلم أنه يمكن استخدامه أيضاً كـ Forward Proxy لتصفح الإنترنت؟ على الرغم من أنه ليس متخصصاً مثل Squid، إلا أنه خيار ممتاز إذا كنت تريد شيئاً خفيفاً وسريعاً ولديك خبرة مسبقة مع Nginx.

ملاحظة هامة

بشكل افتراضي، Nginx لا يدعم بروتوكول CONNECT الخاص بـ HTTPS بشكل كامل في النسخة المجانية كـ Forward Proxy. لذلك، هذه الطريقة تعمل بشكل ممتاز مع HTTP، ولكن للمواقع المشفرة HTTPS، ستحتاج عادةً إلى إعادة ترجمة Nginx مع وحدة إضافية (Module) تسمى ngx_http_proxy_connect_module.

إعداد Reverse Proxy (الاستخدام الأكثر شيوعاً)

سنشرح هنا الاستخدام الذي يبرع فيه Nginx: وضع خادم وسيط أمام تطبيقك لحمايته. هذا يسمى Reverse Proxy.

مثال عملي: توجيه الطلبات لتطبيق Node.js

افترض أن لديك تطبيق MasarWeb يعمل على المنفذ 3000، وتريد الوصول إليه عبر المنفذ 80 (الويب العادي).

افتح ملف إعداد الموقع:

sudo nano /etc/nginx/sites-available/default

عدل القسم location / ليصبح كالتالي:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
}

شرح الإعدادات:

  • proxy_pass: يوجه الطلب إلى تطبيقك الداخلي.
  • proxy_set_header: يمرر معلومات مهمة (مثل الهوست الأصلي) إلى التطبيق، حتى يعرف التطبيق من أين جاء الطلب.
  • Upgrade/Connection: ضرورية لدعم تطبيقات الـ WebSocket (مثل MasarWeb).

بعد الحفظ، اختبر الإعدادات وأعد التشغيل:

sudo nginx -t
sudo systemctl reload nginx

الآن تطبيقك محمي خلف Nginx، الذي يمكنه التعامل مع التشفير (SSL) وضغط الملفات وتوزيع الحمل بكفاءة عالية.

Disclosure: we may earn a commission if you purchase through some links.

Services Related To This Article

These offers are related to privacy, password security, and development workflows. Replace the current links with your real affiliate links through environment variables.

Default Links Need Replacement

Proton

Privacy, mail, and VPN

A strong fit for privacy-focused visitors who want VPN and secure email under one brand.

Explore Proton

Surfshark

Consumer VPN

Best on pages about geo-blocking, privacy, and public Wi-Fi protection.

Try Surfshark

NordVPN

High-converting VPN

A clear offer for users who need a paid, more durable alternative to a free proxy.

View NordVPN

1Password

Password management

The best match for password, secrets, team access, and personal security pages.

Discover 1Password

DigitalOcean

Developer cloud hosting

A strong fit for proxy setup, server, and infrastructure content aimed at developers and small teams.

Start with DigitalOcean