I'm trying to redirect links that look like:
http://example.com/dev/some_project
to their physical location:
http://example.com/dev/some_project/some_project.php
In order to achieve this, I came up with this following rule set:
location / {
try_files $uri $uri/ #folderless-php;
index index.php index.html index.htm;
}
location #folderless-php {
rewrite ^(.*)$ "${uri}/${basename}.php";
}
This, however, will for some reason just invoke an internal server error.
So I tried changing it to:
location / {
set $folderless "${uri}/${basename}.php";
try_files $uri $uri/ $folderless;
index index.php index.html index.htm;
}
This seemed to work with curl, but when I tried this in any browser, I was just offered to download the file that I tried to access, to my astonishment.
What causes this behavior? Is there a way to achieve what I'm trying to do?
You are being given a download as you haven't told Nginx how to handle PHP files. You need to set up the fast-cgi parameters to something like:
location ~ \.php$ {
fastcgi_read_timeout 60000;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$fastcgi_script_name;
include fastcgi_params;
}
Related
I'm using a local nginx server for the first time to set up a website i'm building and i'm having trouble setting up the nginx config to handle url requests the way I want. My website serves multiple php pages as the user navigates through the website. When developing the site initially using a local php server, I used GET requests with window.location.href changes for site navigation. For example:
http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3
However, since its going to be an ecommerce website for a small business, I wanted to handle the URLs in a cleaner and more professional manner.
My site structure looks something like this:
Website:
->index.php
->shop.php
->about.php
->product-page.php
->/css/
->/javascript/
->/php/
I want to configure nginx to route url paths in the following way
www.mywebsite.com -> routes to index.php
www.mywebsite.com/shop -> routes to shop.php
www.mywebsite.com/shop/anything -> routes to shop.php
www.mywebsite.com/about -> routes to about.php
www.mywebsite.com/product -> routes to product-page.php
www.mywebsite.com/product/anything -> routes to product-page.php
I've tried numerous suggestions over a couple of days before asking here but everything failed due to one reason or another, 404s, 500 internal errors, and redirect loops. I'm hoping to gain some inside here while I move onto other aspects of the site, so as to stop beating my head against the wall. Here is the state of my nginx conf at this moment:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location = /shop {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index shop.php;
try_files $uri /shop.php;
}
location /shop/ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
try_files $uri /shop.php;
}
location ~ \.php$ {
try_files $uri =404;
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;
}
}
How could I go about solving this? And if there is a better standard in structuring a website and its URLS please let me know. This is my first website and first time using nginx - so i'm a little naive on best practices.
If you need a certain php script to be responsible for a whole path, you need a config like this:
root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
index /shop.php; # be careful with path to the file here
}
Although I would rather recommend a more traditional and cleaner project structure.
In your project root create two directories: shop and product. Move shop.php and product-page.php into designated folder and rename both to index.php. Your nginx config for this structure will be like this:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
index index.php;
try_files $uri $uri/ =404;
}
location /shop/ {
try_files $uri $uri/ /shop/index.php?$args;
}
location /product/ {
try_files $uri $uri/ /product/index.php?$args;
}
location ~ \.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;
}
I'm trying to set up an nginx environment where legacy code and new MVC-style code can co-exist, so that I can gradually refactor it page by page. The legacy code needs an older version of PHP (it runs best on 5.3, but I had trouble compiling that, so I went with 5.4 and will fix anything that breaks), but it is easily distinguishable by URL, because it has literal file names like http://sub.domain.com/search.php?category=4, etc. instead of new style like http://sub.domain.com/search/category/4 - the key difference is the presence of .php.
The new code runs fine with the following in the nginx config:
server {
listen 80;
server_name *.myproject.dev;
root /var/www/myproject/public;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ ^(.+\.php)(/.*)?$ {
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
(I will admit that I don't completely understand all that code - it came from various guides and such.)
With the help of this great tutorial I compiled and installed PHP 5.4 in its own location listening on port 9001. It works fine using a separate domain for the old code, but what I want to do is use a single domain, but call the old code if .php is found in the URL, and do the requisite rewrite on anything else and use the new code. I found this post on ServerFault and tried incorporating its ideas in my situation like this:
server {
listen 80;
server_name *.myproject.dev;
root /var/www/myproject/public;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ ^(.+\.php)(/.*)?$ {
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
# Anything with ".php" is directed to the old codebase
location ~* \.php {
root /var/www/myproject/oldcode;
fastcgi_pass 127.0.0.1:9001;
}
}
}
But the rewrite adds index.php to the new code, so in the end, everything matches the .php test, which is not the intent. I tried putting those final four lines earlier in the file with several variations, but that didn't help (either a blank page or still only going to the old code location, depending on the details). Does someone know enough about nginx config syntax to help me rearrange it so that it does what I want?
If your new code only uses /index.php and without any path_info, you could use a prefix location:
location ^~ /index.php { ... }
location ~* \.php { ... }
The first location takes precedence due to the ^~ operator. Or an exact match (which also takes precedence):
location = /index.php { ... }
location ~* \.php { ... }
I'm having an issue where when I go to the /public directory it shows the Laravel app as normal, but navigating away to any other page results in it saying
No input file specified.
I am using an Nginx server with PHP 5.5.9 FPM.
I've scoured google for the last 4 hours or so, looking at every tutorial and stackoverflow page for rewriting issues in Laravel however they all yield the same result.
I've even set all the files and folders to 777 so I could see if it was some sort of permissions issue. I've checked the Laravel config and it's all set, I've no idea what is wrong.
Can anyone point me in the right direction?
The last config I tried is below:
server {
listen 80 default_server;
root /usr/share/sites/base;
index index.php
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
I have also tried many others such as:
server {
listen 80;
server_name domain.com;
root /usr/share/sites/base;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~* \.php$ {
# Server PHP config.
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
The error "No input files specified" will nearly always be related to the fact that the wrong path was sent to php.
Looking at your 'last config tried' I can see that fastcgi_param SCRIPT_FILENAMEis not defined in your php location. You should first begin by defining it in the location :
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
}
Furthermore you say that you can reach the app so this means that index.php is working but not when you change page. So the problem should also come from /index.php?$args. Indeed, using this line if I try to reach yourserver.com/test and if 'test' is not a file in your root path nginx will then try request /index.php? (I had this probem). You should try only with /index.php.
EDIT : The solution was that root directive should point to the Laravel public folder, in that case /usr/share/sites/base/public.
Is it any magic rewrite (like it does on Apache) for nginx to be able to rewrite URLs like '/submit.php' to be able to process them from index.php? We have lots of 404 not found errors because of the site structure and all previous URLs were like '/addrate.php', '/my_settings.php', '/profile.php' --> there are over 50 files like this and it would be very unprofessional and code-unwise to create a separate .php file for each of these functions, instead of parsing them all through index.php and using the needed classes like we do with the other rewrites.
Can you please find a solution/give us a suggestion today about this?
I think some info about this is here, but I want the exact reversed result:
http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/
This configuration allow you to handle all URL (non-existing files in file system) with one php script placed in /var/www/example.org/htdocs/index.php
server {
listen 80; ## listen for ipv4
server_name example.org www.example.org;
root /var/www/example.org/htdocs;
access_log /var/log/nginx/example.org.access.log;
error_log /var/log/nginx/example.org.error.log;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ #php;
}
# enable running php files under php fastcgi
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs$fastcgi_script_name;
#fastcgi_param QUERY_STRING $uri;
include fastcgi_params1;
}
location #php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs/index.php; # this script catches all non existing URLs
fastcgi_param QUERY_STRING $uri;
include fastcgi_params1;
}
}
I'm having some issues getting a subdirectory working on my nginx server.
I'm using nginx to serve a wordpress installation as the web root, and trying to run an additional php application at a subdirectory. Wordpress runs fine, but I cannot for the life of me get the application to run in the subdirectory without a 404, 403, or "No input file specified." error with various configurations. I'm sure there is something obvious, but I can't seem to figure it out!
Here is the relevant config:
Any help would be greatly appreciated!
server {
listen myserver.edu:8081;
server_name myserver.edu:8081;
try_files $uri $uri/ /index.php;
location / {
root /path/to/nginx/html/wordpress;
index index.php;
}
location /stacks {
alias /another/path/to/usr/local/share/stacks/php;
index index.php;
}
location ~ \.php$ {
set $php_root /path/to/nginx/html/wordpress;
include fastcgi_params;
fastcgi_pass localhost:8082;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
}
location ~ \stacks.php$ {
set $php_root /another/path/to/usr/local/share/stacks/php;
include fastcgi_params;
fastcgi_pass localhost:8082;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
}
I don't know how to do it using your alias and setting $php_root. I do know how to fix it if you make a symbolic link from the external folder into your wordpress-rootdirectory.
So using the terminal you make a symbolic link so that your stacks-subdirectory is an actual subdirectory:
ln -s /another/path/to/usr/local/share/stacks/php /path/to/nginx/html/wordpress/stacks
As an nginx-config I would use
server {
listen myserver.edu:8081;
server_name myserver.edu:8081;
root /path/to/nginx/html/wordpress;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location /stacks {
try_files $uri $uri/ /stacks/index.php;
}
location ~ \.php$ {
fastcgi_pass localhost:8082;
include fastcgi_params;
}
}
Comment out 'try_files'. Do the sub directories start to work then? Perhaps it is processed before the 'location' directives are considered. If that's the case, then move the 'try_files' into the block for 'location /'.
I think that's a better place for 'try_files' anyway. In the current configuration, it looks like requests for files that don't exist will all be sent to Wordpress, even if they are in the 'stacks' directory.