R
R
redersmp2019-12-08 16:07:35
Nginx
redersmp, 2019-12-08 16:07:35

Convert htaccess to nginx config?

When converting through the site https://winginx.com/en/htaccess

Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1
RewriteRule ^profile/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?page=profile&id=$1
RewriteRule ^@([A-Za-z0-9_.]+)/?$ index.php?page=profile&id=$1
RewriteRule ^@([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.php?page=profile&id=$1&s=$2
RewriteRule ^administrator/editor/theme/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ administrator/editor/index.php?theme=$1&preset=$2
RewriteRule ^administrator/([A-Za-z0-9-]+)/?$ index.php?page=admin&p=$1
RewriteRule ^m_profile/([0-9]+)/([A-Za-z0-9-]+)/?$ mobile.php?page=profile&id=$1
RewriteRule ^chat/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?page=chat&id=$1
RewriteRule ^search/([A-Za-z0-9-]+)/?$ index.php?page=logout
RewriteRule ^search/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?page=logout

the resulting rules after inserting into the config give an error: nginx: [emerg] unknown "script_filename" variable
nginx version/1.14.2
final config
server {
   listen 80;


 server_name mysite.com ;


 # home directory 
 root /opt/WWWRoot/mysite; 
 charset utf-8; 
 index index.php index.html;
 proxy_connect_timeout 600s;
 proxy_send_timeout 600s;
 proxy_read_timeout 600s;
 	
  server_tokens off;

  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-Content-Type-Options nosniff;
  add_header X-XSS-Protection "1; mode=block";

  client_body_buffer_size  512M;
  client_header_buffer_size 1k;
  client_max_body_size 512M;
  large_client_header_buffers 4 8k;

  access_log off;
  error_log /var/log/nginx/mysite.log warn;


---------
autoindex off;

location / {
  if ($script_filename !~ "-d"){
    rewrite ^/([A-Za-z0-9-]+)/?$ /index.php?page=$1;
  }
  rewrite ^/@([A-Za-z0-9_.]+)/?$ /index.php?page=profile&id=$1;
  rewrite ^/@([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ /index.php?page=profile&id=$1&s=$2;
}

location /profile {
  rewrite ^/profile/([0-9]+)/([A-Za-z0-9-]+)/?$ /index.php?page=profile&id=$1;
}

location /administrator {
  rewrite ^/administrator/editor/theme/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /administrator/editor/index.php?theme=$1&preset=$2;
  rewrite ^/administrator/([A-Za-z0-9-]+)/?$ /index.php?page=admin&p=$1;
}

location /m_profile {
  rewrite ^/m_profile/([0-9]+)/([A-Za-z0-9-]+)/?$ /mobile.php?page=profile&id=$1;
}

location /chat {
  rewrite ^/chat/([0-9]+)/([A-Za-z0-9-]+)/?$ /index.php?page=chat&id=$1;
}

location /search {
  rewrite ^/search/([A-Za-z0-9-]+)/?$ /index.php?page=logout;
  rewrite ^/search/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /index.php?page=logout;
}

  ######################################################################

  # -- Cache media images and turn off logging to access log
  location ~ \.(gif|png|swf|js|ico|cur|css|jpg|jpeg|txt|mp3|mp4|ogg|ogv|webm|wav|ttf|woff|eot|svg)$ {
    expires 30d;
    add_header Cache-Control "public";
    access_log off;
  }

  # -- Do not cache document html and data
  location ~ \.(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
  }

  # -- Cache CSS and Javascript
  location ~* \.(?:css|js)$ {
    expires 2d;
    add_header Cache-Control "public";
  }

  # -- Pass off to FastCGI processor
  
  location ~ /\. {
    deny  all;
    access_log off; log_not_found off;
  }

  # Load configuration files for the default server block.
  #include /etc/nginx/default.d/.conf;
  location ^~ /error/ {
 		internal;
    root /opt/WWWRoot/mysite/;
  }

  location ^~ /static/ {
    root /opt/WWWRoot/mysite/;
  }
   
    location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
        deny all;
    }
  
    location ~ ^/(apps/) {
        deny all;
    }


  location = /favicon.ico { access_log off; log_not_found off; }
  location = /robots.txt  { access_log off; log_not_found off; allow all; }


  error_page 403 404 405 /error/404.html;
  error_page 500 502 503 504 /error/500.html;

  
  
    location ~ (index\.php|tiny_mce_gzip\.php)$ {
        
        fastcgi_keep_conn on;
 # for PHP-FPM over socket
 gzip on;
 try_files $uri = 404;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 fastcgi_send_timeout 600s;
 fastcgi_read_timeout 600s;
 fastcgi_intercept_errors on;
 fastcgi_ignore_client_abort on;
 
fastcgi_buffering            on;
  fastcgi_buffer_size          512m;
  fastcgi_buffers              8 512m; 
  fastcgi_busy_buffers_size    512m; 
  fastcgi_temp_file_write_size 512m;
 
    }

  # -- Deny access to .htaccess files, if Apache's document root concurs with nginx's one
  location ~ /\.ht {
    deny  all;
  }
}

But according to the nginx manuals, this is not entirely correct, as it should be something like this
try_files $uri $uri/ /index.php?page=profile&id=$1;
Help me write the correct rewrites please, because there is not enough knowledge ;(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2019-12-08
@SagePtr

Because nginx uses a different approach to determine existence - through try_files.
Something like this (did not test a piece of the config):

location @fallback {
  rewrite ^/([A-Za-z0-9-]+)/?$ /index.php?page=$1;
}
location / {
  try_files $uri @fallback;
}

And even better, if you wrap everything that doesn’t exist in any case on index.php, make only one rule instead of a heap of rewrite, and in index.php already parse the request and break it into parts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question