Hello , i’m facing problem when i tried to reverse proxy using nginx but i’m able to do it on apache2 , i’m really thankfull if someone can convert my apache settings. here’s my apache2 reverse proxy settings
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName host_ip
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
ProxyPass /.well-known !
Alias /.well-known/pki-validation /etc/pki/validation
<Directory /etc/pki/validation/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost Off
ProxyPass / https://example.com/
ProxyPassReverse / https://example.com/
RequestHeader unset Accept-Encoding
<Location "/">
Header edit Location "example.com" "host_ip"
SetEnv filter-errordocs
AddOutputFilterByType SUBSTITUTE text/html
AddOutputFilterByType SUBSTITUTE text/css
AddOutputFilterByType SUBSTITUTE application/javascript
AddOutputFilterByType SUBSTITUTE application/json
Substitute "s/example.com/host_ip/in"
</Location>
</VirtualHost>
i’ve tried this settings
server {
listen 80;
listen [::]:80;
server_name host_ip www.host_ip;
access_log off;
error_log /var/log/nginx/test.error.log;
root /var/www/index;
index index.php index.html index.htm;
location / {
#proxy_set_header Host example.com;
#proxy_set_header X-Forwarded-Host example.com;
#proxy_set_header X-Forwarded-Server example.com;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_ignore_headers Set-Cookie;
proxy_intercept_errors on;
proxy_pass https://example.com;
proxy_buffering off;
proxy_buffer_size 16k;
proxy_busy_buffers_size 24k;
proxy_buffers 64 4k;
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf|woff2|m4a|cur|heic|tiff|webm|mp3|aac|webp)$ {
include common/headers-http.conf;
include common/headers-https.conf;
add_header "Access-Control-Allow-Origin" "*";
access_log off;
log_not_found off;
expires max;
proxy_pass https://example.com;
}
location = /robots.txt {
access_log off;
log_not_found off;
proxy_pass https://example.com;
}
}
}
i’ve tried to commented out and in the proxy header still no luck . ( bad gateway 502)
and the basic settings
server {
listen 80;
location / {
# rewrite tag html link '{$Host_ip}' '{host}'
sub_filter '<a href="http://example.com' '<a href="http://$host';
sub_filter_once off;
proxy_pass http://example.com;
#proxy_cache_bypass $http_upgrade;
# Proxy headers
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
# proxy_set_header X-Forwarded-Host $host;
# proxy_set_header X-Forwarded-Port $server_port;
# Proxy timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
i need to reverse domain URL , and not IP URL. Much thanks :)
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
Converting an Apache reverse proxy configuration to Nginx can be a bit tricky, as the syntax and directives differ between these two web servers.
Your Apache configuration is doing several things:
/var/www/html./.well-known from the proxy and serving it locally.https://example.com.example.com with host_ip in the response.Here’s an equivalent Nginx configuration:
server {
listen 80;
server_name host_ip www.host_ip; # Use your actual host IP or domain name here
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Root directory
root /var/www/html;
index index.php index.html index.htm;
# Handling the /.well-known directory
location ~ /.well-known {
alias /etc/pki/validation;
try_files $uri $uri/ =404;
}
# Main reverse proxy configuration
location / {
proxy_pass https://example.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Adjust timeouts as needed
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
# Substitution
sub_filter 'example.com' 'host_ip'; # Adjust this as per your needs
sub_filter_once off;
}
# Additional location blocks for specific file types
# Adjust this part according to your needs
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
proxy_pass https://example.com;
access_log off;
expires max;
}
# Custom handling for robots.txt
location = /robots.txt {
proxy_pass https://example.com;
access_log off;
}
}
This configuration tries to replicate your Apache settings as closely as possible. Here are a few key points:
root directive is set to /var/www/html, mirroring your Apache DocumentRoot.location ~ /.well-known block serves static content from a specified directory, similar to your Apache Alias directive.location / block is set up to reverse proxy requests to https://example.com, similar to your Apache ProxyPass directive.sub_filter directive is used to replace example.com with host_ip in the response body. Note that sub_filter only works with certain MIME types, typically text-based content like HTML, CSS, and JavaScript.Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.