Nginx rewrite assistance request - nginx

To make a long story short, I have an osCommerce installation that has been running on an Apache server for years using osCommerce's "Search Engine Friendly links" so that the links look like product_info.php/products_id/26 instead of product_info.php?products_id=26.
Well, now I've moved to an Nginx server with PHP5 and the Search Engine Friendly links just won't work for me now. BUT I have links all across the web pointing to my shop using the "friendly" links, so I have to figure out a way to redirect any url that is like this:
product_info.php/products_id/26
into this
product_info.php?products_id=26
and
index.php/cPath/19
into
index.php?cPath=19
and
product_info.php/cPath/19/products_id/207
into
product_info.php?cPath=19&products_id=207
Anyone good with Nginx rewrite rules, or who knows how to make the osCommerce built-in SEO urls work with Nginx? The osCommerce urls don't require any htaccess rewrite rules; it looks like it's all taken care of using PHP code.
nginx.conf:
user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server_names_hash_bucket_size 10240;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*.vhost;
}
site.com.vhost:
server {
listen *:80;
server_name site.com www.site.com;
root /var/www/clients/client23/web5/web;
index index.php index.html index.htm;
#location ~ \.shtml$ {
# ssi on;
#}
error_log /var/log/ispconfig/httpd/site.com/error.log;
access_log /var/log/ispconfig/httpd/site.com/access.log combined;
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location /stats {
index index.html index.php;
auth_basic "Members Only";
auth_basic_user_file /var/www/clients/client23/web5/web/stats/.htpasswd_stats;
}
location ^~ /awstats-icon {
alias /usr/share/awstats/icon;
}
location ~ \.php$ {
try_files /5lbe4fd76b7f89.htm #php;
}
location #php {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
# Static Contents
location ~* ^.+.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
log_not_found off;
expires 1y;
}
# CSS and JS
location ~* ^.+.(css|js)$ {
access_log off;
log_not_found off;
}
server_tokens off;
error_page 400 /400.php;
error_page 401 /401.php;
error_page 403 /403.php;
error_page 404 /404.php;
error_page 405 /405.php;
error_page 500 /500.php;
error_page 502 /502.php;
error_page 503 /503.php;
recursive_error_pages off;
fastcgi_intercept_errors on;
# Inaccessible locations
location ~ ^/includes/.*\.php$ { return 404; }
location ~ ^/administrator/includes/.*\.php$ { return 404; }
location ^~ /administrator/backups { return 404; }
location ^~ /download { return 404; }
location ^~ /cgi-bin { return 404; }
location ^~ /mail { return 404; }
location ^~ /pub { return 404; }
location ^~ /sql { return 404; }
location ^~ /temp { return 404; }
location ~ /\. { deny all; access_log off; log_not_found off; }
location ~ \.(tpl|log|sql)$ {deny all; access_log off; log_not_found off; }
location / {
#if (!-e $request_filename) { rewrite ^(.*)$ /index.php; }
fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
rewrite ^/(.*)-(.*).html$ /index.php?cPath=$2&$query_string;
rewrite ^/(.*)-m-([0-9]+).html$ /index.php?manufacturers_id=$2&$query_string;
rewrite ^/(.*)-pi-([0-9]+).html$ /popup_image.php?pID=$2&$query_string;
rewrite ^/(.*)-t-([0-9]+).html$ /articles.php?tPath=$2&$query_string;
rewrite ^/(.*)-a-([0-9]+).html$ /article_info.php?articles_id=$2&$query_string;
rewrite ^/(.*)-pr-([0-9]+).html$ /product_reviews.php?products_id=$2&$query_string;
rewrite ^/(.*)-pri-([0-9]+).html$ /product_reviews_info.php?products_id=$2&$query_string;
rewrite ^/(.*)-i-([0-9]+).html$ /information.php?info_id=$2&$query_string;
}
location /administrator/ {
auth_basic "Members Only";
auth_basic_user_file /var/www/clients/client23/web5/web/administrator/.htpasswd;
location ~ \.php$ {
try_files /wj70wjksdkj2jiejlsjdslj.htm #php;
}
}
}

Here we go:
rewrite "^/product_info.php/products_id/(\d+)" /product_info.php?products_id=$1 permanent;
rewrite "^/index.php/cPath/(\d+)" /index.php?cPath=$1 permanent;
rewrite "^/product_info.php/cPath/(\d+)/products_id/(\d+)" /product_info.php?cPath=$1&products_id=$2 permanent;

Related

Wordpress permalink after migrating to Nginx

My permalinks are ~~/%category%~~/%postname%/~~.html~~.
I migrated from Apache to Nginx.
Then my config file I added in
server{
location /mydirectory/ {
try_files $uri $uri/ /mydirectory/$uri.html;
}
But my browser goes on going to the 404 page.
I tried many codes found after deleting cache but still no success.Any help will be much appreciated
Thank you
I desactivate all my plugins and was able to change my permalinks into
/%postname%/
Here is my my.site.com config (my site is in a sub directory Vie/) :
server {
listen 80;
server_name my.site.com;
location ~ /(vie|\|Vie|Vie)/(.*) {
return 301 https://my.site.com/Vie;
}
return 301 https://my.site.com/Vie$request_uri;
}
server {
listen 443 ssl;
server_name my.site.com;
root /var/www/my.site.com/html;
index index.php index.html;
ssl_certificate /etc/letsencrypt/live/krouus.company/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/krouus.company/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-CAMELLIA256-SHA:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-CAMELLIA128-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA256';
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_dhparam /etc/nginx/dhparam.pem;
add_header Strict-Transport-Security "max-age=31536000;";
location /Vie/ {
index index.php;
try_files $uri $uri/ /Vie/index.php?$args;
}
location / {
try_files $uri $uri/ /Vie/index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
location ~* commun {
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 ~ /\.ht {
deny all;
}
location ~ (^|/)\. {
return 403;
}
location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
expires max;
log_not_found off;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
## Set the OS file cache.
open_file_cache max=1000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
}
location ~* \.ini$ {
deny all;
return 404;
}
error_page 404 /404.html;
location /404.html {
internal;
}
location ~* /(?:uploads|files)/.*\.(html|htm|shtml|php|js|swf|py|jsp|asp|sh|cgi)$ {
deny all;
}
if ($request_method !~ ^(GET|POST|HEAD)$ ) {
return 444;
}
location ~* wp-includes/theme-compat/ {
deny all;
}
location ~* wp-includes/js/tinymce/langs/.*.php {
deny all;
}
location /wp-includes/ {
internal;
}
location ~* .(pl|cgi|py|sh|lua|asp)$ {
return 444;
}
location ~* /(wp-config.php|readme.html|license.txt|nginx.conf) {
deny all;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location /xmlrpc.php {
deny all;
}
}
Try this instead. As i was also facing same problem,hope its work for you too.
location /mydirectory{
try_files $uri $uri/ /mydirectory/index.php;
}
Well in fact I solved my problem. I read attentively my ocnfiguration and removed
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
And now my Wordpress works.
Cheerio!

Struggling with nginx.conf rewrites

Trying to get Nginx on Jelastic to play nice with nice with socialengine. There's a myriad of forum posts and other documentation but I've hit a brick wall.
Unmodified nginx.conf
#user nobody;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Websocket support
#upstream websocket {
# server 127.0.0.1:<PORT>;
#}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
include /etc/nginx/aliases.conf;
#location /ws {
# proxy_pass http://websocket;
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "Upgrade";
#}
location / {
root /var/www/webroot/ROOT;
index index.html index.htm index.php;
location ~ \.php$ {
location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/webroot/ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/webroot/ROOT;
}
}
index index.php index.html index.htm;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ /\. { deny all; access_log off; log_not_found off; }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location ~ \.php$ {
location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/webroot;
}
}
include /etc/nginx/conf.d/*.conf;
}
This is apparently what I am trying to accomplish in order for the rewrites for socialengine to work.
location / {
index index.php index.html index.htm;
if ( $request_uri ~ "/" ) {
rewrite ^(.*)$ /index.php?rewrite=1 last;
}
if (!-e $request_filename) { rewrite ^(.*)$ /index.php?rewrite=1 last;}
}
location /install/ {
index index.php;
if (!-e $request_filename) { rewrite ^(.*)$ /install/index.php?rewrite=1 last; }
}
This seems super simple to me at the outset, but for some reason any way I have attempted to add these lines(or parts thereof) has prevented the nginx server from restarting.

wordpress nginx config wont let me open pages using ?page_id=

I am trying to configure wordpress and came to the step when i want to use sample or my own pages. Unfortunately it seems like i didnt configure nginx correctly but i just cant seem to find howto. Either its an old or irrelevant to my version of nginx(1.2.1-2.2+wheezy2) or just incomplete. Can somebody provide a sample nginx wordpress config or just tell me which of the following is most correct to get it working?
location /wordpress {
try_files $uri $uri/ /etc/wordpress/index.php?$args;
}
or
location /wp/wp-content/ {
alias /usr/share/wordpress/wp-content/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
or something else?
Here is the nginx conf file I am using on my local mac for local development in case it helps:
user matt staff;
worker_processes 4;
events {
worker_connections 768;
}
http {
client_max_body_size 100M;
include mime.types;
types_hash_max_size 2048;
default_type text/plain;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
index index.html index.php;
log_format main '[$time_local]: $domain - "$request" '
'status:$status bytes:$body_bytes_sent "$http_referer" ';
access_log /Users/matt/Library/Logs/nginx/access.log main;
error_log /Users/matt/Library/Logs/nginx/error.log;
upstream www-upstream-pool {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name ~^(?:(?<subdomain>\w*)\.)?(?<domain>\w+)\.wp$;
set $basepath "/Users/matt/pls-sites";
set $rootpath "${domain}";
if ($domain ~ 'wordpress'){
set $rootpath "wordpress/httpdocs/web";
}
root $basepath/$rootpath;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args; # /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass www-upstream-pool;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 300;
}
}
}

Nginx returns 404 on cached html files

i tried to add caching on my all .html files without index page, but always when i did some changes my files went to 404 not found page.
This is what i have on my default config, without any changes that i made and not worked .
server {
listen 80;
server_name site.net;
root /storage/www/site.net;
access_log /var/log/nginx/site.net.access.log;
error_log /var/log/nginx/site.net.log info;
index index.php;
error_page 404 = /404.php;
if ($host = 'www.site.net' ) {
rewrite ^/(.*)$ http://site.net/$1 permanent;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
# This matters if you use drush
location = /backup {
deny all;
}
# Very rarely should these ever be accessed outside of your lan
location ~* \.(txt|log)$ {
allow 127.0.0.1;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ #rewrite;
expires max;
}
location ~ ^/sites/.*/private/ {
access_log off;
internal;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*).html$ /index.php?s=$1;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}}
Try this:
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*).html$ /index.php?s=$1 last;
}

Can we use nginx for both wordpress and drupal?

I have a debian machine on which I installed nginx for my web server worker process. But we just need to change a little for wordpress in default nginx configuration which is located /etc/nginx/sites-enabled/ !
/etc/nginx/sites-enabled/wordpress.com
/etc/nginx/sites-enabled/drupal.com
Can I know sample nginx configurations for those two websites under single debian machine.
You can run multiple sites using nginx, analogous to an apache vhost.
In /etc/nginx/sites-enabled, you can add two vhosts
wordpress.example.com
server {
listen 80;
server_name wordpress.example.com;
root /var/www/wordpress;
# if ($http_host != "www.example.com") {
# rewrite ^ http://www.example.com$request_uri permanent;
# }
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
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;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
and
drupal.example.com
server {
listen 80;
server_name drupal.example.com;
root /var/www/drupal;
# if ($http_host != "www.example.com") {
# rewrite ^ http://www.example.com$request_uri permanent;
# }
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
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;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9001;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Resources