0

I have few virtual hosts (sites) running on this single server.

Right now, on this root virtualhost i have a forum (running on Docker) but served by Nginx mysite.com and I have its AMP pages being served on /amp route which is mysite.com/amp. These AMP pages are basically 1 single index.php file & they all are handled by this 1 file. These are served by PHP using Nginx.

What I want is, when a user hits any of these requests matching below patterns: (like if ANY URL on this domain ending with ?amp=1

mysite.com?amp=1

mysite.com/t/my-topic/121?amp=1

mysite.com/c/CategoryCaseInsensitive/13?amp=1

mysite.com/u/john?amp=1

mysite.com/u/john/summary?amp=1

THEN

I want to redirect this request and send it to my AMP page (which is running on PHP file and will be then served/handled by index.php which is present in /var/www/amp ) . Right now the PHP code is being served on /amp but i want to serve it on mysite.com?amp=1 so any URL preceding ?amp=1

I have tried this code but its not seem to working for all cases:

#if ($arg_amp) {
#   return 302 /amp$request_uri;
#}

Below is my current NGINX config file for this virtual host:

    #Vhost Config Server, serving Ruby on Rails App on Docker on domain root
server {
    listen 443 ssl http2;
    ssl on;
    ssl_certificate         /var/www/cert/mysite.pem;
    ssl_certificate_key     /var/www/cert/mysite.key;


    server_name mysite.com www.mysite.com;
    location / {
        proxy_ssl_server_name       on;
        proxy_pass       http://localhost:PORT;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_redirect off;

        # Socket.IO Support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_ssl_protocols         TLSv1 TLSv1.1 TLSv1.2;
       
       #if ($arg_amp) {
       #   return 302 /amp$request_uri;
       #}

    } 
    #Serving PHP code on /AMP route
    location @amp {
           rewrite ^/amp(.*) /amp/index.php?q=$1;
        }
    #will match any prefix for amp, amping, or amp/anything/any  

    location /amp {

    index index.php;
    try_files $uri $uri/ @amp;
    alias /var/www/amp;

#PHP config for Nginx
     location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }

}
    #/amp route ends
}

The above config works perfect (and serves PHP) on URLs like mysite.com/amp/t/topic-name/123 and on mysite.com/amp but i want it work for mysite.com/t/topic-name/123?amp=1 and on mysite.com?amp=1

Is this not possible in Nginx?

Faizan
  • 111
  • 3
  • 2
    You should try: `if ($arg_amp) { rewrite ^ /amp/index.php?q=$uri last; }` – Richard Smith Sep 30 '20 at 07:22
  • let me try this now @RichardSmith hold on – Faizan Sep 30 '20 at 07:27
  • @RichardSmith it worked! thanks, u r a legend! Do i still need to keep my @ amp and /amp routes though ? i noticed if i remove @ amp it still works but if remove location /amp then it fails. – Faizan Sep 30 '20 at 09:21

0 Answers0