Nginx rewrite all directory requests to index.php - redirect

Okay I'm kind of n00b on Nginx, and I've browsed through here and couldn't piece together an answer. SO here is what i got
server {
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location /dojump {
rewrite ^/dojump/(.*)$ /dojump/index.php/$1 break;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
This is on a wordpress setup the first location block should pass all file requests to the wordpress bootstrap.
the location /dojump block is supposed to be for an outbound redirect script i have. I want to catch the arguments and pass them to the index.php script
like /dojump/cnn.com
to /dojump/index.php/cnn.com
it works with apache with this simple .htaccess line inside the dojumps folder
RewriteRule ^(.*)$ index.php/$1 [L]
however, I get an nginx error in the error log
/usr/share/nginx/www/dojump/index.php/cnn.com" failed (20: Not a directory)
Any help?
Thanks

Try passing the url as a GET parameter.
rewrite ^/dojump/(.*)$ /dojump/index.php?url=$1 break;

Related

NGINX not executing perl scripts?

I'm running NGINX on Centos 8 and I can't get it to execute a perl script, it just keeps downloading the script.
I have several domains on this server and it runs php scripts and such fine.
I have installed perl on the server,
"This is perl 5, version 26, subversion 3 (v5.26.3) built for x86_64-linux-thread-multi"
I tried adding this server block to the conf.d file:
location ~ \.pl|cgi$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Without this bit added to the server block, it simply downloads the file; with this bit added to the server block, I get a 502 bad gateway. So I'm sure something in there is incorrect.
I took it almost exactly from the PHP version of the bit, which looks like this:
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;
}
Any ideas what I'm doing wrong? Thank you!!!
You are trying to execute the Perl scripts with PHP by using the exact same FastCGI backend which you use for PHP. This will not work. The FastCGI backend for PHP will look for PHP code only and since there is none in the Perl scripts it will simply deliver the content the same it would do for HTML files.
Instead you need to have another FastCGI backend specifically for Perl. See here for an example on how to do this or one of the many other sites which cover this topic.

Why does my Nginx alias redirect not work (Goes to 404)?

I have my main site at x.com (# /var/www/x.com/index.html)
# MAIN LOCATION
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#autoindex on;
proxy_set_header X-Real-IP $remote_addr;
}
I want /v2 to redirect to another local dir (# /var/www/x.com/v2/public/index.html)
# Redirect beta site
location /v2 {
alias /var/www/x.com/v2/public;
}
This seems like it should work, but instead, it's going to my 404 site. Not sure if this matters, but here is my root above both of them:
# path
root /var/www/throneoflies.com/html;
I tried ordering "/v2" both above "/" and below - didn't seem to make a difference. I read I shouldn't use 'root' instead of 'alias' because it's a different schema (/v2/public/ and not just /v2/).
EDIT: Seems like it should be working - I've read a lot since this post. Here is my full file:
server {
# MAIN >>
# SSL configuration
listen 443 default_server ssl;
server_name www.x.com;
error_page 404 /error/404.html;
server_tokens off;
# SSL
ssl_certificate /etc/ssl/x.com/cert.pem;
ssl_certificate_key /etc/ssl/x.com/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_session_tickets off;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
# path
root /var/www/x.com/html;
#root /var/www/x.com/v2/public; #works!
# Add index.php to the list if you are using PHP
#index index.php index.html index.htm index.nginx-debian.html;
index index.html;
# 404 handling - no cache
location = /404.html {
add_header Cache-Control "no-cache" always;
}
# Redirect beta site : Why doesn't it work if the outer block root changed to the same path works? I can REPLACE my "/" with this v2 path np. However, alias at /v2 does not work.
location = /v2 {
alias /var/www/x.com/v2/public;
#root /var/www/x.com/v2/public;
#try_files $uri $uri/ =404;
}
# MAIN LOCATION
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#autoindex on;
proxy_set_header X-Real-IP $remote_addr;
# DENY .HTACCESS ACCESS
location ~/\.ht {
deny all;
}
}
This should obviously be caused by the = in your location = /v2, which will only match requests where the $uri is exactly /v2; the correct way would most likely be to remove =.
Additionally, note that if using location /v2 {alias /var/www/x.com/v2/public;}, then folks would also be able to access things like /var/www/x.com/v2/public.bak through /v2.bak; if that's not intentional, then you would be better off having trailing slashes, as appropriate.

nginx redirct non existing directory to an existing one

A given wordpress site had once a given link http://www.website.com/link/ which existed but now doesn't exist and I need to redirect it (301) to an existing one: http://www.website.com/link-existing/
I tried a lot of things which doesn't work:
location /link/ {
try_files $uri $uri/ /link-existing/;
}
location /link/ {
return 301 /link-existing/;
}

Nginx folder redirection without changing the url

I want to redirect an old path to a new one without changing the url and keeping the suffix of the url for the redirection:
dev-osm.blah.com/NYC/v2/site/links?key=a12345
redirect to
dev-osm.blah.com/NYC/v1/site/links2?key=a12345
server {
server_name dev-osm.blah.com
.
.
location ^~ /NYC/v2/site/links {
rewrite ^ /NYC/v1/site/links2
}
Use something like the following in your server config
location /NYC/v2
proxy_pass: /NYC/v1/

Nginx language redirect: /de/test/test.php to /test/test.php?

I'm trying to configure Nginx on a new server. I have a number of PHP scripts (f.e. /test/test.php) and I want to use this scripts "as is" (default language, English), as well as with language redirecting. Example - when "/de/test/test.php" is requested,
nginx writes a cookie (lang=de)
and returns "/test/test.php" (without modifying URI, so that visitor remains on "/de/test/test.php"
Any help is greatly appreciated! I already lost several nights fighting with this, and I'm getting desperate enough to cancel new server and return back to shared hosting.
Thanks!
Create a sub location under your php location:
location ~ ^.+\.php$ {
# Return '400 Bad Request' for malformed URL exploits
location ~ \..*/.*\.php$ {
return 400;
}
location ~ ^/de/(.+)\.php {
add_header Set-Cookie lang=de;
rewrite ^/de/(.+)\.php$ /$1.php last;
}
# Your proxy or fastcgi code to process PHP
...
}
I know it's old but in case anyone else finds it useful, below is how I solved it (with some help from a friend). I set LANG variable in nginx, which I then pick up in PHP $_SERVER['LANG'].
# languages
location ~ ^/(af|sq|ar|hy|az|be|bs|bg|cs|zh|da|de|nl|el|en|et|fa|fi|fr|ka|he|hi|hr|hu|id|it|ja|kk|ko|lv|lt|nb|no|pl|pt|ro|ru|sk|sl|es|sr|sv|th|tk|tr|uk|uz|vi|bp)/
{
set $lang $1;
rewrite ^/[^/]+/(.+)$ /$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_read_timeout 600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param LANG $lang;
}