In a subfolder on a domain I want to install a wordpress blog. I use nginx. The URL to access the blog should be like this: example.com/blog
site config looks as follows:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location /blog {
alias /var/www/example.comblog/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ /blog/.+\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
The wordpress files reside in the folder
/var/www/example.comblog/html. When accessing example.com/blog,
the browser shows a 404 error.
In /etc/php5/fpm/php.ini I adapted this: cgi.fix_pathinfo=0
nginx version: nginx/1.6.2
/var/log/nginx/error.log does not show anything of interest
UPDATE 1:
After setting error logging to debug, (among others) the following lines appear. Maybe this helps:
open index "/var/www/example.comblog/html/index.php"
internal redirect: "/blog/index.php?"
rewrite phase: 1
test location: "/blog"
test location: ~ "/blog/.+\.php$"
using configuration "/blog/.+\.php$"
http script var: "/blog/index.php"
trying to use file: "/blog/index.php" "/var/www/example.com/html/blog/index.php"
The internal redirect seems incorrect? And in the last line there should be /var/www/example.comblog/html/blog/index.php instead of /var/www/example.com/html/blog/index.php. I suspect this is the reason for the 404. Because the index.php does not exist at /var/www/example.com/html/blog/index.php.
Update 2:
Okay there seems to be a long standing issue with using alias together with try_files.
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 need to have a php application inside my default server configuration.
Also this application it was previously allocated on a apache server with rewrite rules on the .htaccess file that I translated to nginx.
I'm trying to configure it all like that:
server {
listen [::]:443;
server_name _;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
location /admin {
alias /var/www/sefoanco/html;
index index.php index.html index.htm;
try_files $uri $uri/ #admin;
rewrite ^(/admin/.*)/login/ /login/controller.php break;
rewrite ^(/admin/.*)/observacions/ /observacions/controller.php break;
rewrite ^(/admin/.*)/usuari/ /usuari/controller.php break;
rewrite ^(/admin/.*)/llistat/ /llistat/controller.php break;
location ~ /admin/.+\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
}
location #admin {
rewrite /admin/(.*)$ /admin/$1 last;
}
}
If I access to localhost/admin/login I get a 403 forbidden error.
If I add the controller.php to the index files, it answer correctly with the php response, so I think that the php is well configured.
So I think that I forget something to config.
Finally the problem was that the application are not made to work inside a path. If I see the application configuration I saw the following:
$_DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
So, is easy to make it working on paths but the application doesn't work with relative paths so after make a call inside login, will search the auth method inside the root path.
So is needed to rewrite some parts of the application before it can work inside a path.
Anyway, I get the login page working just deleting rewrite rules and adding controller.php on the index param. Something like should work:
server {
listen [::]:443 ;
server_name _;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html controller.php;
location /admin {
try_files $uri $uri/ =404;
}
location ~ \.php($|/) {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
}
This is my config here for Nginx. I have a domain named tstdmn and two Laravel projects first tstdmn.com project and second florist project I want to deploy florist project into the tstdmn.com/florist, I set it all but it returns a blank page at tstdmn.com/florist what's my issue here?! And I know the problem is with my Nginx configuration because I switch the florist project to the main project and it works, it's not from my Laravel configurations
root /var/www/html/tstdmn.com/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name tstdmn.com www.tstdmn.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /florist {
alias /var/www/html/florist/florist_backend/public;
try_files $uri $uri/ #laravel1;
location ~ \.php {
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
}
}
location #laravel1 {
rewrite /florist/(.*)$ /florist/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
And so another route of project show's blank like /register
I solved it by someone's solution at laracasts:
using subdomain instead of routing
server { listen 80; listen [::]:80;
root /var/www/html/florist/florist_backend/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name florist.tstdmn.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
..... and some code not related
address of question in laracast https://laracasts.com/discuss/channels/servers/nginx-multiple-projects-in-one-domain-errors
The scenario is that I'd like to use Wordpress as a backend API provider for our Ember.js frontend app.
The Ember.js frontend needs to be served from the root, and the Wordpress instance ideally would be reachable by going to a subdirectory. So for example on localhost it would be http://localhost and http://localhost/wordpress
On the disk the two are deployed in /srv/http/ember and /srv/http/wordpress respectively.
I was trying to assemble the configuration going by the example on the Nginx site:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
The config:
http {
upstream php {
server unix:/run/php-fpm/php-fpm.sock;
}
server {
listen 80;
server_name localhost;
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
location /wordpress {
root /srv/http/wordpress;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
}
}
However this is obviously not the correct solution.
Upon trying to access the address http://localhost/wordpress/index.php I get the following in the logs:
2016/05/01 17:50:14 [error] 4332#4332: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /wordpress/index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "localhost"
The recipe isn't clear about where to put the root directive for the location of wordpress. I also tried with adding index index.php, which doesn't help either.
(Serving the Ember app works fine.)
From your question it seems that the location ~ \.php$ block is used by WordPress alone. However, it needs a root of /srv/http in order to find the script files for URIs beginning with /wordpress under the local path /srv/http/wordpress.
As there are two locations which both use the same WordPress root, it is possibly cleaner to make /srv/http the default (that is, inherited from the server block) and move root /srv/http/ember; into a separate location / block.
server {
listen 80;
server_name localhost;
root /srv/http;
location / {
root /srv/http/ember;
index index.html;
try_files $uri $uri/ /index.html?/$request_uri;
}
location /wordpress {
index index.php;
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}
Notice that the default URI in location /wordpress is /wordpress/index.php and not /index.php as you originally had.
I have explicitly set SCRIPT_FILENAME as it may or may not appear in your fastcgi.conf file.
fastcgi_split_path_info has been removed as it is unnecessary in your specific case, and I think it would actually break WordPress the way you had it.
Hi every one I am new to magento. I've been trying to install magento 1.9.0.0 on with nginx server on Ubuntu 14.04LTS but I can't get started. I can see default page see below
magento default page
but whenever I try to log in it, server fails with UNABLE TO CONNECT error.
here is my virtual host
server {
listen 80;
listen [::]:80;
server_name www.mymagento.com;
root /var/www/magento;
index index.php;
#need it to execute php
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
I already tried other solution like updating core_config_data web/unsecure/base_url and web/secure/base_url they contain my base url.
I tried reloading cache. there is nothing in nginx log neiher.
Thank you for helping me :-)
You have nothing to redirect URIs to the Magento common front handler. As a minimum you should add:
location / {
try_files $uri $uri/ /index.php;
}
But I suggest that you check this site for more.
Hi as Richard SMITH said, my virtual host was missing some lines to somehow redirect to front handler. As I have just begin to use Nginx, I won't be able to explain each lines but after strugling with Nginx this config works for me (I also needed to add self signed certificate to config to make it work).
server {
listen 80;
listen [::]:80;
listen 443 default ssl;
server_name www.mymagento.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /var/www/magento;
index index.php;
location / {
index index.html index.php;
autoindex on;
#If missing pass the URI to Magento's front handler
try_files $uri $uri/ #handler;
expires max;
}
#need it to execute php
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
## Magento uses a common front handler
location #handler {
rewrite / /index.php;
}
}