V
V
viktorpolyjopa2021-09-11 13:48:22
Nginx
viktorpolyjopa, 2021-09-11 13:48:22

How to switch from apache to nginx?

Good afternoon, I decided to switch from apache to nginx, the project does not have a single entry point, but such entries in htaccess
RewriteRule ^results/$ /results.php [L]
and so on. Translated htaccess to nginx config
via https://winginx.com .
Got something like this:

server {
    listen                    %ip%:%httpport%;
    listen                    %ip%:%httpsport% ssl;
    server_name               %host% %aliases%;
    root                      '%hostdir%';
    #limit_conn               addr 64;
    autoindex                 off;
    index                     index.php index.html index.htm;

    ssl_certificate           '%sprogdir%/userdata/config/cert_files/server.crt';
    ssl_certificate_key       '%sprogdir%/userdata/config/cert_files/server.key';
    #ssl_trusted_certificate  '';

    # Force HTTPS
    #if ($scheme ~* ^(?!https).*$) {
    #    return 301 https://$host$request_uri;
    #}

    # Force www.site.com => site.com
    #if ($host ~* ^www\.(.+)$) {
    #    return 301 $scheme://$1$request_uri;
    #}

    # Disable MIME sniffing
    add_header X-Content-Type-Options 'nosniff' always;

    # HSTS
    #add_header Strict-Transport-Security 'max-age=2592000' always;

    # Disable access to hidden files/folders
    location ~* /\.(?!well-known/) {
        deny all;
        log_not_found off;
        access_log off;
    }

    # Disable access to backup/config/command/log files
    location ~* (?:\.(?:bak|co?nf(ig)?|in[ci]|log|sh|sql|tar|t?gz)|~)$ {
        deny all;
    }

    location ~* ^.+\.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv|svgz?|ttf|ttc|otf|eot|woff2?)$ {
        expires 1d;
        access_log off;
    }

    location / {
        # Force index.php routing (if not found)
        #try_files $uri $uri/ /index.php?$query_string;

        # Force index.php routing (all requests)
        #rewrite ^/(.*)$ /index.php?/$1 last;

        location ~ \.php$ {
            try_files      $fastcgi_script_name =404;

            #limit_conn   addr 16;
            #limit_req    zone=flood        burst=32 nodelay;

            #add_header   X-Frame-Options   'SAMEORIGIN' always;
            #add_header   Referrer-Policy   'no-referrer-when-downgrade' always;

            # CSP syntax: <host-source> <scheme-source>(http: https: data: mediastream: blob: filesystem:) 'self' 'unsafe-inline' 'unsafe-eval' 'none'
            #add_header   Content-Security-Policy-Report-Only  "default-src 'self'; report-uri https://site.com/csp/" always;
            #add_header   Content-Security-Policy  "default-src 'self'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self'; manifest-src 'self'; media-src 'self'; object-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; base-uri 'none'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests" always;

            fastcgi_pass   backend;
            include        '%sprogdir%/userdata/config/nginx_fastcgi_params.txt';
        }
    }

    location = /results {
      rewrite ^(.*)$ /results.php break;
    }


    # Service configuration (do not edit!)
    # ----------------------------
    location /openserver/ {
        root      '%sprogdir%/modules/system/html';
        autoindex off;
        index     index.php index.html index.htm;
        %allow%allow all;
        allow 127.0.0.0/8;
        allow ::1/128;
        allow %ips%;
        deny all;
        location ~* ^/openserver/.+\.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|webm|svgz?|ttf|ttc|otf|eot|woff2?)$ {
            expires 1d;
            access_log off;
        }
        location /openserver/server-status {
            stub_status on;
        }
        location ~ ^/openserver/.*\.php$ {
            try_files      $fastcgi_script_name =404;
            fastcgi_index  index.php;
            fastcgi_pass   backend;
            include        '%sprogdir%/userdata/config/nginx_fastcgi_params.txt';
        }
    }
    # End service configuration
    # ----------------------------
}


But when you go to /results it just gives 404 Not Found. That is, it tries to open the results folder, not the results.php file. Although there is such an entry in the config
location = /results {
      rewrite ^(.*)$ /results.php break;
    }

Help me to understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-09-11
@viktorpolyjopa

RewriteRule ^results/$ /results.php [L]
In Nginx it would be
rewrite ^/results/$ /results.php last;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question