Question

convert apache reverse proxy to nginx

Posted August 31, 2021 34 views
Nginx

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

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!