1

I know there are a lot of threads about this topic but none seemed to work for me. I am not sure if it is because I have to do it to both server blocks, or I was doing something wrong. Please help me out.

Below is my nginx config on the remote server on Amazon, the first block represent the backend and the second block represent the frontend:

limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:60m; # nginx 1.1.9 or higher
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:60m rate=20r/s;

server {
    listen 8080;

    server_name api.commonskudev.com;

    root /var/www/api/public;

    gzip on;

    server_tokens off;

    index index.php;

    client_max_body_size 64M;
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    send_timeout 300;

    try_files $uri $uri/ @rewrite-staging;

    location @rewrite-staging {
        rewrite ^ /index.php;
    }

    location ~* \.php$ {
        include fastcgi_params;
#        fastcgi_param HTTPS on;
        fastcgi_pass 127.0.0.1:9000;
    }
}

server {
    listen 443 default_server ssl;
    server_name rightsleeve.commonskudev.com;
    # rewrite ^ http://commonskudev.com/maintenance.html;

    ssl on;
    # ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
    # ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
    ssl_certificate /etc/nginx/csku-dev.crt;
    ssl_certificate_key /etc/nginx/csku-dev.key;

    gzip on;
    gzip_proxied any;

    client_max_body_size 64M;
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    send_timeout 300;

    root /var/www/web;

    server_tokens off;

    index index.php;

    error_page 404 /404.php;

    rewrite ^/v[0-9]+\.[0-9]+/(.*)$ /$1;

    rewrite ^/project/([0-9]+) /project.php?job_number=$1;

    if ($http_referer ~* "semalt\.com") {
        return 444;
    }

    location ~* ^(css|js|images|files) {
        expires 1y;
        add_header Pragma public;
        add_header Cache-Control public;
    }

    location ~* \.(ttf|woff) {
        add_header Access-Control-Allow-Origin "*";
    }

    location ~* \.php$ {
        if (!-f $document_root/$fastcgi_script_name) {
            return 404;
        }

        limit_conn conn_limit_per_ip 35;
        limit_req zone=req_limit_per_ip burst=35;

        include fastcgi_params;
        fastcgi_param HTTPS on;
        fastcgi_pass 127.0.0.1:9000;
    }

    location ~* \.(png|jpg|dst|xls) {
        try_files $uri $uri/ @nofile;
    }
    location @nofile {
        rewrite ^ /images/404.png;
    }

    if ($uri ~* ^/([a-zA-Z0-9_\-]+)$) {
        rewrite ^/([a-zA-Z0-9_\-]+) /vanity_url.php?mask=$1&$args;
    }

    location /v1 {
        proxy_pass http://api.commonskudev.com:8080;
        proxy_set_header Host $host;
    }
}

server {
    listen 80 default_server;
    server_name rightsleeve.commonskudev.com;
    rewrite ^(.*) https://$host$1 permanent;
}

2 Answers 2

1

You can use a rewrite similar to this:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

As taken from this SO answer.

Sign up to request clarification or add additional context in comments.

3 Comments

I saw this before but I want as little modification as possible on my existed config. I understand the concept but applying it did not help.
You cannot do this without modifying config files. Also, you want to replace with another or completely remove the extension?
@peixotorms would be good if I can completely remove the extension. The config I posted was not done by me and as such I want to do as minimal damage to the config as possible and remove the extension.
0

Use this:

server {
  listen 80;
  server_name www.example.local;
  root /var/www/vhosts/example/httpdocs;

  index index.html index.htm index.php;

  location / {
    try_files $uri $uri/ @ext;
  }

  location ~ \/\.php {
    rewrite "^(.*)\/.php" $1.php last;
  }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
  }

  location @ext {
    rewrite "^(.*)$" $1.php;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.