Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Artificial Intelligence

How to Setup OpenAI Reverse Proxy with Nginx

by Cloudbooklet
3 months ago
in Artificial Intelligence, Linux
Openai Reverse Proxy With Nginx
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

Learn how to set up OpenAI reverse proxy with Nginx in this comprehensive guide. Follow our step-by-step instructions for seamless integration of OpenAI's API with Janitor AI or other services using reverse proxy

ADVERTISEMENT

OpenAI, a renowned leader in AI research, offers an API that enables developers to leverage their powerful language models. This article will provide a step-by-step guide on setting up OpenAI reverse proxy with Nginx on a Ubuntu 22.04 machine with a sub-domain and Let’s Encrypt free SSL. This setup allows you to efficiently integrate AI capabilities into your applications like Janitor AI, Venus AI and more.

Table of Contents

  1. Benefits of an OpenAI Reverse Proxy
  2. Prerequisites
  3. Initial Setup
  4. Install Nginx for OpenAI Reverse Proxy
  5. Configure OpenAI Reverse Proxy with Nginx
  6. Configuring Proxy Cache (Optional)
  7. Enable Nginx Configuration for OpenAI Reverse Proxy
  8. Secure the Setup with Free SSL
  9. Verify OpenAI Reverse Proxy With Nginx
  10. Conclusion

Also Check: How to setup OpenAI Reverse Proxy with Docker in Hugging Face

Benefits of an OpenAI Reverse Proxy

OpenAI reverse proxy in combination with Nginx has some advantages listed below:

ADVERTISEMENT
  • Performance: By configuring a reverse proxy, you can cache OpenAI API responses, reducing latency and improving overall performance for your users.
  • Scalability: The reverse proxy acts as an intermediary between your application and the OpenAI API, enabling you to scale your AI integration seamlessly.
  • Security: A reverse proxy can add an extra layer of security by shielding sensitive API keys and protecting your backend infrastructure from direct external access.

Let’s begin to start configuring OpenAI reverse proxy with Nginx.

You might also like

Google Bard Extension

Google Bard Extensions: How to Link Your Gmail, Docs, Maps, and More to an AI Chatbot

1 hour ago
Validator Ai

Validator AI: The AI Powered Business Idea Validator

1 day ago

Prerequisites

  1. A machine with Linux distro with external IP so that we can configure subdomain and install SSL.
  2. A user with sudo privileges or root access.

Initial Setup

Start by updating the packages to the latest version available.

sudo apt update
sudo apt upgrade -y

Install Nginx for OpenAI Reverse Proxy

You can install Nginx easily by a single command.

ADVERTISEMENT
sudo apt install nginx

Verify the nginx installation using the below command.

sudo service nginx status

You will see an output of the status of Nginx (active or failed).

ADVERTISEMENT

Configure OpenAI Reverse Proxy with Nginx

Now you need to remove the default Nginx configuration that is shipped with Nginx installation.

sudo rm -rf /etc/nginx/sites-enabled/default
sudo rm -rf /etc/nginx/sites-available/default

Create a new configuration for OpenAI reverse proxy.

ADVERTISEMENT

Create a new file inside the Nginx sites-available directory.

sudo nano /etc/nginx/sites-available/reverse-proxy.conf

Copy the entire configurations listed below to the editor.

ADVERTISEMENT

Make sure to replace the below listed ones.

  • OPENAI_API_KEY with the one you get from OpenAI platform.
  • YOUR_DOMAIN_NAME with your domain name.
proxy_ssl_server_name on;

server {
    listen 80;
    server_name YOUR_DOMAIN_NAME;

    proxy_set_header Host api.openai.com;
    proxy_http_version 1.1;
    proxy_set_header Host $host;

    proxy_busy_buffers_size   512k;
    proxy_buffers   4 512k;
    proxy_buffer_size   256k;

    location ~* ^\/v1\/((engines\/.+\/)?(?:chat\/completions|completions|edits|moderations|answers|embeddings))$ {
        proxy_pass https://api.openai.com;
        proxy_set_header Authorization "Bearer OPENAI_API_KEY";
        proxy_set_header Content-Type "application/json";
        proxy_set_header Connection '';
        client_body_buffer_size 4m;
    }
}

Hit CTRL + X followed by ENTER to save and exit the editor.

Enable the newly created Nginx configuration.

Configuring Proxy Cache (Optional)

You can also configure caching for performance if you need. You need to just replace the above code we added with the one below.

proxy_ssl_server_name on;

proxy_cache_path /server_cache levels=1:2 keys_zone=openai_cache:10m max_size=1g inactive=4d use_temp_path=off;

log_format cache_log '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" '
                    'Cache: $upstream_cache_status';
                    
server {
    listen 80;
    server_name YOUR_DOMAIN_NAME;

    proxy_set_header Host api.openai.com;
    proxy_http_version 1.1;
    proxy_set_header Host $host;

    proxy_busy_buffers_size   512k;
    proxy_buffers   4 512k;
    proxy_buffer_size   256k;

    location ~* ^\/v1\/((engines\/.+\/)?(?:chat\/completions|completions|edits|moderations|answers|embeddings))$ {
        proxy_pass https://api.openai.com;
        proxy_set_header Authorization "Bearer OPENAI_API_KEY";
        proxy_set_header Content-Type "application/json";
        proxy_set_header Connection '';
        proxy_cache openai_cache;
        proxy_cache_methods POST;
        proxy_cache_key "$request_method|$request_uri|$request_body";
        proxy_cache_valid 200 4d;
        proxy_cache_valid 404 1m;
        proxy_read_timeout 8m;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;
        access_log /dev/stdout cache_log;
        proxy_ignore_headers Cache-Control;
        add_header X-Cache-Status $upstream_cache_status;
        client_body_buffer_size 4m; 
    }
}

Enable Nginx Configuration for OpenAI Reverse Proxy

sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf

Test Nginx configuration.

sudo nginx -t

Restart Nginx for the changes to take effect.

Also Read: How to Setup and Use Janitor AI API with OpenAI Reverse Proxy

Secure the Setup with Free SSL

Now we will install Let’s Encrypt free SSL and secure your requests.

Install Certbot using the below command.

sudo apt install python3-certbot-nginx

Now you can install SSL using the certbot command.

Make sure to replace your email and domain name with the real ones.

Important: Your domain should point to the IP address of your server, otherwise SSL installation will get failed.

sudo certbot --nginx --redirect --no-eff-email --agree-to-tos -m [email protected] -d yourdomain.com

Now you will have the SSL installed for you.

Verify OpenAI Reverse Proxy With Nginx

Now you have your Nginx server configured to work with OpenAI API. To test if this works you can form the url with v1/chat/completions.

These are some of the endpoints listed below.

  • POST /v1/chat/completions
  • POST /v1/completions
  • POST /v1/edits
  • POST /v1/embeddings
  • POST /v1/moderations
  • POST /v1/answers

If you make requests to the required endpoint you will get the response as requested.

Conclusion

That’s it! You’ve successfully set up an OpenAI API reverse proxy using Nginx on Ubuntu 22.04 . You have also installed and configured SSL to handle security measures for protecting your API key and requests.

Tags: ChatGPTNginxUbuntu
Share14Tweet9SendShare
Cloudbooklet

Cloudbooklet

Comments 1

  1. Avatar Of John john says:
    2 months ago

    Looks great, but I am getting 403s. Does it have something to do with clouldfare?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Chatgpt To Translate

How to Use ChatGPT to Translate Your Website or Blog

1 day ago
Fantasy Minecraft Servers

5 Best Fantasy Minecraft Servers in 2023

1 day ago
Ai Statistics And Trends

AI Statistics and Trends: What You Need to Know in 2023

1 day ago
Block Youtube Ads

How to Block YouTube Ads on Android TV in 2023 (6 Easy Methods)

1 day ago

Follow Us

Trending Articles

Ai Girl Generator

7 Best AI Girl Generators for Creating Realistic and Beautiful AI Girls

September 19, 2023

HeyGen AI: Free AI Video Generator to Create Amazing Videos

How to Create and Customize Stunning Contact Poster on iPhone

Top 7 Free Dating Sites for Men in 2023

10 Best AI Prompts for Writers to Improve Website SEO

Microsoft Editor vs Grammarly: Which is the Best Grammar Checker?

Popular Articles

Undressai_1

7 Free Undress AI Tools to Remove Clothes from Images

September 19, 2023

Jenni AI: How to Use AI Writing Assistant for Amazing Content

How to Make an AI Cover Song with Singify: A Step-by-Step Guide

9 Best Bulk Email Service Providers of 2023

7 Best Deepswap AI Free Online Tools to Create FaceSwap Videos and Photos

6 Best AI Voice Cloning Tools that You Need to Try in 2023

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.