I managed to make my index.php script to run properly when I get a url like:
http://myserver.com/?something.m3u8
Still, I need to remove the ? from it.
Any ideas?
My Nginx config looks like:
server {
listen 6868 default_server;
root /var/tmp/mfl;
index index.php;
# Make site accessible from http://localhost/
server_name localhost;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Apparently is was an one line solution
location / {
try_files $uri $uri/ /index.php?$args; #appended this line
}
Related
I have project which uses nginx and php-fpm container.
In docker-compose, I can use this nginx setting fastcgi_pass php:9000;
(php is container name of php-fpm)
in nginx.conf
server {
listen 80;
server_name xxx.example.com;
index index.php index.html index.htm;
root /var/www/public;
client_max_body_size 200M; # 413 Request Entity Too Large
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
#fastcgi_pass 0.0.0.0:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
However now I want to use set(nginx, php-fpm in ECS.
In Ecs there is not container name.
And php-fpm container accept only
netstat -an|grep LISTEN
tcp6 0 0 :::9000 :::* LISTEN
Now, I tried few patterns in nginx.conf
fastcgi_pass php:9000;
#fastcgi_pass 0.0.0.0:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
but either one doesn't work.
How can I set?
I'm trying to modify my NGINX config to strip the .html extension from URI's before they are passed to my PHP based CMS.
In other words when a visitor enters:
http://www.example.com/foo.html
I want the URI to be changed to/;
http://www.example.com/foo
Without doing an actual browser redirect. This is easy enough to accomplish in Apache, but I can't seem to crack the nut in NGINX. Here is what I have in my config file that doesn't seem to work.
location ~ \.html {
rewrite ^(/.*)\.html(\?.*)?$ $1$2 last;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
Using this code the REQUEST_URI that PHP picks up is still /foo.html.
Possible answer to my own question, or at least a workaround.
So apparently $request_uri is always going to contain the original URI, not the rewrite - that is contained in $uri. So to workaround this I'm using a variable to store and pass the modifed URI to PHP. I'm not really thrilled with this solution though.
location ~ \.html {
rewrite ^(/.*)\.html(\?.*)?$ $1$2 last;
}
location / {
set $new_uri $uri
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param REQUEST_URI $new_uri;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
I have a django running on example.com, i need add a Wordpress to my server, would be example.com/blog, perhaps doesnt work (404 error):
listen 80;
server_name www.example.com;
location ^~ /blog/ {
root /www/blog;
index index.html index.htm index.php;
try_files $uri =404;
location ~ \.php {
root /www/blog;
fastcgi_split_path_info ^(.*\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
}
}
location / {
uwsgi_pass unix:/tmp/myapp.sock;
include /www/webapp/system/uwsgi_params; # the uwsgi_params file you installed
uwsgi_read_timeout 300;
}
I ran in this problem too. Here, you can check my location config.
location /blog {
root /var/www/html;
try_files $uri $uri/ /blog/index.php?$args;
index index.php;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
Adapted for your config:
location /blog {
root /www/blog;
try_files $uri $uri/ index.php?$args;
index index.php;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
#Levi configuration give me some light, i need to modify because i'm getting 404 error or No input file especified.
location /blog {
alias /www/blog;
try_files $uri $uri/ index.php?$args;
index index.php;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/blog$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
When I visit the http://example.com or http://example.com/index.html still goes to handleURL.html
This is what I have
server {
listen 80;
listen [::]:80;
root /var/www/vhosts/example.com/public;
index index.html;
server_name example.com www.example.com;
access_log /var/log/nginx/example.com/access.log;
error_log /var/log/nginx/example.com/error.log;
rewrite "^/([a-z0-9]{4,8})$" /forward.php?shortcode=$1;
rewrite "^/.{10,500}$" /handleURL.html;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want to display different html file if the url has characters between 10,500.
Nginx might not care but it looks like the regular expression should not be in quotes. Unless your going to use the entered string in your replacement url you don't need to capture it, so the parenthesis are unnecessary.
If you want to choose a different file for each string you can use the captured string in the replacement string. e.g. rewrite "^/(.{10,500})$" /wiki/$1 last; would match example.com/agreatpage.html and turn it into example.com/wiki/agreatepage.html
To simply redirect to another page based on length something like rewrite "^/.{10,500}$" /Redirect.html last; should work
References:
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
Edit:
After further reading nginx is okay if the regular expression is enclosed in double quotes and you must do so if the expression contains { or }, otherwise the parser misinterprets these as opening a block and closing a block.
Edit2:
The only problem I see at the moment is:
rewrite "^/([a-z0-9]{4,8})$" /forward.php?shortcode=$1;
rewrite "^/.{10,500}$" /handleURL.html;
to the best of my knowledge should be:
rewrite "^/([a-z0-9]{4,8})$" /forward.php?shortcode=$1 last;
rewrite "^/.{10,500}$" /handleURL.html last;
The only way I can fix this remove all the rewrites and add index.php and pass everything to index.php I ended up with the following configuration.
server {
listen 80;
listen [::]:80;
root /var/www/vhosts/example.com/public;
index index.php index.html;
server_name example.com www.example.com;
log_format compression '$host - $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /var/log/nginx/example.com/access.log compression;
error_log /var/log/nginx/example.com/error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Thank you guys!
I have a website with URLs corresponding to PHP files:
www.mysite.com/cat.php?id=stuff
These PHP files don't exist anymore, how can I do a 301 redirect (for SEO reasons) to the new URL :
www.mysite.com/stuff
I tried
rewrite ^/cat\.php\?id=stuff http://www.mysite.com/stuff? permanent;
But it does not work, I get a "No input file specified".
Thank you for your help!
EDIT:
More about my config (website is powered by Wordpress):
index index.php;
root /var/www/mydirectory;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
The issue is that you've added a ? to the end of your rewritten directory, so nginx is telling PHP to serve http://yourdomain.com/stuff?/index.php which doesn't exist.
Assuming mysites.com was a typo and you're redirecting to the same domain, try this:
rewrite ^/cat\.php\?id=(.*)$ /$1/ permanent;
There are a lot of issues with using rewrite and try_files together, I have a working config using these, something like:
I think the rule is that your rewrite rule has to come before try_files, so try this:
index index.php;
root /var/www/mydirectory;
location = / {
rewrite ^/cat\.php\?id=(.*)$ /$1/ permanent;
}
location ^(.*)$ {
try_files $uri $uri/ /index.php?$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}