I have move my site to nginx but can get .htaccess roules right
below ius my code
server {
listen 80;
server_name mysite.com;
root /usr/share/nginx/mysite;
index index.php index.html index.htm;
location / {
rewrite ^/(.*)$ /members.php?id=$1 last;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
i want to display user url to be
mysite.com/jonny
instead of
mysite.com/memner.php?id=jonny
when i use
rewrite ^/(.*)$ /members.php?id=$1 last;
site don't load please help.
got fix for this
if (!-e $request_filename) {
rewrite /(.*) /members.php?id=$1 last ;
}
Related
i have a problem that i can't figure it out,
so my problem is this, i am trying to locate diferent rewrite rules on a subfolder which is the mobile version. site.com is the main site and site.com/mobile/ is the mobile site, what i want to do is to add different rewrite to both locations, is this possible?
server {
listen 111.111.111:80;
server_name site.com www.site.com;
index index.php index.html index.htm;
root /home/admin/web/site.com/public_html;
access_log /var/log/nginx/domains/site.com.log combined;
access_log /var/log/nginx/domains/site.com.bytes bytes;
error_log /var/log/nginx/domains/site.com.error.log error;
location / {
rewrite ^/index.html$ / permanent;
rewrite ^/([^/]*)_([a-zA-Z0-9]+)/$ /watch.php?vid=$2;
rewrite ^/([^/]*)_([a-zA-Z0-9]+).html$ /watch.php?vid=$2;
rewrite ^/fpembed-(.*).swf$ /fpembed.php?vid=$1;
rewrite ^/uploads/thumbs/(.*)-social.(jpg|gif|png)$ /social-thumb.php?vid=$1;
rewrite ^/rss.xml$ /rss.php last;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
location /mobile/ {
alias /home/admin/web/site.com/public_html/mobile/;
rewrite ^/index.html$ / permanent;
rewrite ^/category.html$ /category.php;
rewrite ^/([^/]*)_([a-zA-Z0-9]+).html$ /watch.php?vid=$2;
rewrite ^/fpembed-(.*).swf$ /fpembed.php?vid=$1;
rewrite ^/rss.xml$ /rss.php last;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
can someoane tell me what i'm doing wrong here? Thanks a lot
I migrate apache to nginx.
In apache I put .htaccess inside folder, but nginx hasn't this.
I have folder /admin and it can only by accessed by User Agent: Administrator
I do:
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
And it works, but .php files isn't interpreted.
How can I do this?
Edit:
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
location / {
}
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/default.d/*.conf;
}
There is problem with the specification about agent callback instead of "Administrator" it should be (Administrator) rest of the code seems to be ok. If you have added try_files $uri =404; additionally then remove it as it don't required. return 404 already defined.
Try this one
server {
listen 80;
server_name myserver.com;
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
}
location ~ /admin {
if ($http_user_agent !~* "Administrator") {
return 404;
}
try_files $uri =404;
}
include /etc/nginx/default.d/*.conf;
}
restart nginx service before test!
I have apache server with htaccess file like this:
RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^test/\$([^/]+)/([^/]+\.php)$ test/$2?VIRTUAL_DIRECTORY_NAME=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^test/([^/]+)/(.*)$ $1/$2 [L]
And I am trying to convert it for nginx server.
I cant figure out how to transfor virtual directory to correct format.
This is my nginx config file:
server {
listen 80;
listen 443 ssl;
server_name admin.dev;
root "/home/vagrant/admin";
index index.html index.htm index.php;
charset utf-8;
rewrite_log on;
location /api/ {
rewrite ^/api/(.*)$ /api.php?$1 last;
}
location /res_partners/ {
rewrite ^res_partners/\$([^/]+)/([^/]+\.php)$ res_partners/$2?VIRTUAL_DIRECTORY_NAME=$1 last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/admin.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/admin.dev.crt;
ssl_certificate_key /etc/nginx/ssl/admin.dev.key;
}
Any help?
Thanks
The first rule seems to rewrite /test/$something/somescript.php to /test/somescript.php?VIRTUAL_DIRECTORY_NAME=$something and these are literal $ characters. Your location ~ \.php$ { ... } container already matches any URI that ends with .php. So the first rule can be placed at the top of that container:
location ~ \.php$ {
rewrite ^/test/\$([^/]+)/([^/]+\.php)$ /test/$2?VIRTUAL_DIRECTORY_NAME=$1&$args break;
...
}
The second rule seems to rewrite /test/somedir/something to /somedir/something (where something is not a php script).
This can be achieved with a prefix location:
location /test/ {
rewrite ^/test/([^/]+)/(.*)$ /$1/$2;
}
we have prestashop running smoothly on nginx;
when we enabled multi language on the site, the below issue is faced:
usually after payment, without multi lang, you are redirected back to this page:
http://www.domain.com/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c04910968
on our apache environemnt, this works well
on nginx,
what happened when multi language was enabled is when you reach the above link, the lang parameter is added to the url (when it should not):
http://www.domain.com/en/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c049109689ecf36499b
or
http://www.domain.com/fr/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c049109689ecf36499b
which causes the user to fall on a prestashop error "input file not specified"
the above redirection should not happen, once an order is done, the following URL must work without redirection nor adding the 'en' or 'fr' code before index.php:
http://www.domain.com/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c049109689ecf36499b
the nginx configuration is as follows:
location /favicon {
alias /var/www/vhosts/11footballclub.com/httpdocs/img/facicon.ico;
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/vhosts/11footballclub.com/httpdocs$fastcgi_script_name;
fastcgi_param PHP_VALUE max_execution_time=180;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite "^/c/([0-9]+)(\-[_a-zA-Z0-9-]*)/(.*)\.jpg$" /img/c/$1$2.jpg last;
rewrite "^/c/([_a-zA-Z-]+)/(.*)\.jpg$" /img/c/$1.jpg last;
rewrite "^/([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/(\P{M}\p{M}*)*\.jpg$" /img/p/$1-$2$3.jpg last;
rewrite "^/([0-9]+)\-([0-9]+)/(\P{M}\p{M}*)*\.jpg$" /img/p/$1-$2.jpg last;
rewrite "^/([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$1$2.jpg last;
rewrite "^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$1$2$3.jpg last;
rewrite "^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
#rewrite "^/index.php\?controller=http(.*)$" $1 last;
rewrite "^/([0-9]+)\-(\P{M}\p{M}*)+\.html(.*)$" /index.php?controller=product&id_product=$1$3 last;
rewrite "^/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=category&id_category=$1$3 last;
rewrite "^/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$" /index.php?controller=product&id_product=$2$4 last;
rewrite "^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$" /index.php?controller=supplier&id_supplier=$1$3 last;
rewrite "^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$" /index.php?controller=manufacturer&id_manufacturer=$1$3 last;
rewrite "^/content/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=cms&id_cms=$1$3 last;
rewrite "^/content/category/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=cms&id_cms_category=$1$3 last;
rewrite "^/module/([_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)$" /index.php?fc=module&module=$1&controller=$2 last;
rewrite ^/page-not-found$ /index.php?controller=404 last;
rewrite ^/address$ /index.php?controller=address last;
rewrite ^/addresses$ /index.php?controller=addresses last;
rewrite ^/authentication$ /index.php?controller=authentication last;
rewrite ^/best-sales$ /index.php?controller=best-sales last;
rewrite ^/cart$ /index.php?controller=cart last;
rewrite ^/contact-us$ /index.php?controller=contact-form last;
rewrite ^/discount$ /index.php?controller=discount last;
rewrite ^/guest-tracking$ /index.php?controller=guest-tracking last;
rewrite ^/order-history$ /index.php?controller=history last;
rewrite ^/identity$ /index.php?controller=identity last;
rewrite ^/manufacturers$ /index.php?controller=manufacturer last;
rewrite ^/my-account$ /index.php?controller=my-account last;
rewrite ^/new-products$ /index.php?controller=new-products last;
rewrite ^/order$ /index.php?controller=order last;
rewrite ^/order-follow$ /index.php?controller=order-follow last;
rewrite ^/quick-order$ /index.php?controller=order-opc last;
rewrite ^/order-slip$ /index.php?controller=order-slip last;
rewrite ^/password-recovery$ /index.php?controller=password last;
rewrite ^/prices-drop$ /index.php?controller=prices-drop last;
rewrite ^/search$ /index.php?controller=search last;
rewrite ^/sitemap$ /index.php?controller=sitemap last;
rewrite ^/stores$ /index.php?controller=stores last;
rewrite ^/supplier$ /index.php?controller=supplier last;
location ~* \.(gif)$ {
expires 2592000s;
}
location ~* \.(jpeg|jpg)$ {
expires 2592000s;
}
location ~* \.(png)$ {
expires 2592000s;
}
location ~* \.(css)$ {
expires 604800s;
}
location ~* \.(js|jsonp)$ {
expires 604800s;
}
Add this location block to your config file.
location ~ (/lang1|/lang2)/index\.php$ {
try_files $uri $uri/ /index.php?$args;
}
Where lang1 and lang2 are languages used in your website (for example 'en', 'ru' and etc.). You can add more languages by appending |/language_name inside brackets.
P.s. don't forget to restart/reload your nginx server after modifications.
Here's an example of configuration that works pretty well with Nginx, PrestaShop v1.6.1.x (two languages enabled: EN/FR), PHP-FPM 7.2 and an SSL certificate from Let's Encrypt.
If you copy/paste this example (in your /etc/nginx/sites-enabled folder), please remember to:
Replace 'myshop.com' by your domain name
Replace 'myadminfolder' by your PrestaShop admin folder name
Replace '(/fr|/en)' with the language codes you are actually using
Generate your SSL certificate using Let's Encrypt certbot tool (optional, remove this section if you don't want to use SSL)
Make sure your PHP-FPM socket file is located in /var/run/php/php7.2-fpm.sock, otherwise modify this path
Thanks #Canarius for the tip!
server {
listen [::]:80;
listen 80;
server_name myshopname.com www.myshop.com
root /var/www/myshopname.com/;
index index.php index.html;
access_log /var/log/nginx/myshopname.com.access_log;
error_log /var/log/nginx/myshopname.com.error_log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
auth_basic off;
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 1;
gzip_buffers 16 8k;
gzip_http_version 1.0;
gzip_types application/json text/css application/javascript;
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
location ~ (/fr|/en)/index\.php$ {
try_files $uri $uri/ /index.php?$args;
}
try_files $uri $uri/ /index.php?$args;
location /myadminfolder/ {
if (!-e $request_filename) {
rewrite ^/.*$ /myadminfolder/index.php last;
}
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_keep_conn on;
include /etc/nginx/fastcgi_params;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.myshopname.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.myshopname.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
I'd like to protect the root with passwd and make the urlfriendly rewrite for modx cms.
only the rewriting works but the password won't be requested.
my ispconfig nginx directives looks like this
location / {
index index.html index.php
auth_basic "Protected Area";
auth_basic_user_file /var/www/clients/client21/web22/web/htpasswd;
client_max_body_size 0;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 600;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
client_max_body_size 0;
}
seems like the auth_basic won't be executed or overwriten by the rewrite rule. anyone got a idea?
after alot of ppl viewing this question but i didn't received a answer here i post the solution to the problem. The problem was that the basic auth needs to be done in the php directive.
location / {
index index.html index.php
client_max_body_size 0;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
auth_basic "Protected Area";
auth_basic_user_file /var/www/clients/client21/web22/web/htpasswd;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 600;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
client_max_body_size 0;
}
Try this set of rules. No PHP directives and leverages FURLS.
location / {
auth_basic "Restricted";
# Add path to .htpasswd file
auth_basic_user_file /var/www/clients/client21/web22/web/.htpasswd;
# if the protected location is modx, then ...
try_files $uri $uri/ #modx-rewrite;
# otherwise, if static files, you’d use
# try_files $uri $uri/ =404;
# This line should always be placed at the end
try_files $uri $uri/ #modx-rewrite;
}