V
V
Vasya2019-07-17 22:03:49
Nginx
Vasya, 2019-07-17 22:03:49

How to make correct htaccess redirect/config with apache/nginx reverse-proxy?

Hello.
Redirect 301 in htaccess for some reason sends Apache to the port, more precisely, nginx does not intercept and does not substitute its port.
Why is that ?
checking for ubuntu
nginx config

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.php;
        server_name _;
        location / {
                proxy_pass http://127.0.0.1:8888/;
                proxy_redirect off;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

apache config /etc/apache2/sites-available/000-default.conf
Listen 8888
<VirtualHost *:8888>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        <Directory /var/www/html>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted
        </Directory>
</VirtualHost>

.htaccess
Redirect 301 /red1 /red2
Then I do wget -dO- 127.0.0.1/red1
I get

---request begin---
GET /red1 HTTP/1.1
User-Agent: Wget/1.20.1 (linux-gnu)
Accept: */*
Accept-Encoding: identity
Host: 127.0.0.1
Connection: Keep-Alive
-- -request end---
HTTP request sent. Waiting for response...
---response begin---
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.9 (Ubuntu)
Date: Wed, 17 Jul 2019 18:49:07 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 311
Connection: keep- alive
Location : http://127.0.0.1:8888/red2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2019-07-17
@haramba

if nginx is not sure, Apache should always work, the same 404s and so on are usually processed by cms itself and not by nginx, so we will separate this into a separate rule error_page 401 403 404 405 500 502 503 = @fallback;
and we do not use the trifile at all

server {
######################################################################
## Server configuration
######################################################################
        listen *:80;
        server_name bots.klondike.digital www.bots.klondike.digital   ;
        root /var/www/bots.klondike.digital/web;


######################################################################
## Enable gzip for proxied requests and static files
######################################################################
    # Enable gzip for proxied requests and static files
    gzip on;
    gzip_proxied any;
    gzip_vary on;
    gzip_http_version 1.1;
    gzip_types application/javascript application/json text/css text/xml;
    gzip_comp_level 4;

######################################################################
## Log configuration
######################################################################
#Все логи отключены
        error_log /dev/null crit;
        access_log off;

######################################################################
## Errors send to apache2
######################################################################
# у апача своих алиасов куча,  а так же некоторая статика отдается
# средствамси php, по этому все ошибки обрабатывать только apache2
        error_page 401 403 404 405 500 502 503 = @fallback;
        location @fallback {
                proxy_pass              http://127.0.0.1:82;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

                }

######################################################################
## Locations configuration
######################################################################
#Отключаем логирование ошибок No such file or directory
## Disable .htaccess files

        location ~ /\.ht {
                deny all;

                access_log off;
                log_not_found off;
        }
        ##
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        ##
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
        ##
######################################################################
# сервисы  на сайте phpmyadmin почта и letxencrypt
        location /phpmyadmin/ {
                deny all;
                # поставить пароль на phpmyadmin
                return 555;
                root  /usr/share/phpmyadmin/;
        }
        ##
        location /webmail/ {
                rewrite ^/(.*)$ https://$http_host:8080/$1 permanent;
        }
        # letsencrypt
         location /.well-known/acme-challenge/ {
                alias /usr/local/ispconfig/interface/acme/;
                default_type text/plain;

         }
# static content
# Отдаем статику напрямую с nginx
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|flv|mp3)$ {
                root  /var/www/bots.klondike.digital/web;
                access_log off;
                expires max;
                gzip_static on;
        }

# default location
        location / {
                index index.php index.html index.htm;
                proxy_pass              http://127.0.0.1:82;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
       


}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question