DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • GenAI: From Prompt to Production
  • Grounding Gemini With Google Search and Other Data Sources
  • Exploring Embeddings API With Java and Spring AI
  • AI-Powered Professor Rating Assistant With RAG and Pinecone

Trending

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Setup OpenAI Reverse Proxy With NGINX for ChatGPT

Setup OpenAI Reverse Proxy With NGINX for ChatGPT

ChatGPT OpenAI reverse proxy with NGINX with step-by-step instructions for seamless integration to applications like Janitor AI or other services.

By 
Pappin Vijak user avatar
Pappin Vijak
·
Oct. 11, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Benefits of an OpenAI Reverse Proxy

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

  • 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.

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 with a single command.

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).

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.

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.

Make sure to replace the below-listed ones.

  • OPENAI_API_KEY with the one you get from the OpenAI platform.
  • YOUR_DOMAIN_NAME with your domain name.
Nginx
 
/etc/nginx/sites-available/reverse-proxy.confproxy_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 to. You just need to replace the above code we added with the one below.

Nginx
 
/etc/nginx/sites-available/reverse-proxy.confproxy_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.

Secure the Setup With Free SSL

Now we will install it. 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 fails.

sudo certbot --nginx --redirect --no-eff-email --agree-to-tos -m yourmail@mail.com -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.

AI API Relational model

Published at DZone with permission of Pappin Vijak. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GenAI: From Prompt to Production
  • Grounding Gemini With Google Search and Other Data Sources
  • Exploring Embeddings API With Java and Spring AI
  • AI-Powered Professor Rating Assistant With RAG and Pinecone

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!