I have a LEMP server and I'm looking to install RespondCMS (http://respondcms.com/documentation/install-on-digital-ocean). I'm running into some difficulties with the mod_rewrite section for an API. I've tried several iterations but have been unable to get it to show an "API works" message that indicates the PHP app is working. I get a 404. Thus far, I have settled on the following set-up in my /etc/nginx/sites-enabled/. I'm thinking it's not accessing the /api folder correctly. Any help in understanding how it's all interacting and how to make it work is appreciated.
app.domain.com
server {
listen 80;
server_name app.domain.com;
root /srv/www/domain_app/app;
index index.php index.html index.htm;
access_log /var/log/nginx/respond.access.log;
error_log /var/log/nginx/respond.error.log;
location /api/ {
try_files $uri $uri/ /api/dispatch.php$args;
}
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /srv/www;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
domain.com
server {
listen 80 default_server;
root /srv/www/html;
index index.php index.html index.htm;
server_name domain.com;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.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;
}
}
The URL http://app.domain.com/api/dispatch.php is processed by the location ~ \.(hh|php)$ block. And that block seems to be configured (reasonably) correctly.
You should move include fastcgi_params; above any fastcgi_param directive to avoid the latter being inadvertently overridden. And the fastcgi_index directive is redundant in this context.
Both the location / and location /api/ blocks have issues. The alias directive is unnecessary and wrong. All three locations inherit their root from the outer server block. You should delete both alias directives.
In your try_files $uri $uri/ $uri.php /api/dispatch.php$args; statement, the $uri.php element will cause the PHP file to be downloaded rather than executed. Only the last element of a try_files directive can take an action that is not processed within the same location block. If you really need to execute extension-less PHP URIs and have a default PHP URI too, you will probably need to use a named location.
A useful resource for all nginx directives is here.
Related
I have two sites in my centos 7 VPS. One is laravel 5.6 (let say, def.com) and the other is static html site (let say, abc.com).
The html site is running smoothly with this config:
sites-available/abc.com.conf
server {
listen 80;
server_name server_name abc.com www.abc.com;
# note that these lines are originally from the "location /" block
#root /var/www/abc.com/html;
#index index.php index.html index.htm;
location / {
root /var/www/abc.com/html;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
But when I am adjust the config for laravel project, like below, it come with 404
sites-available/def.com.conf
server {
listen 80;
server_name server_name def.com www.def.com;
# note that these lines are originally from the "location /" block
#root /var/www/def.com/public;
#index index.php index.html index.htm;
location / {
root /var/www/def.com/public;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am using this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-on-centos-7
replace this
try_files $uri $uri/ =404;
with
try_files $uri $uri/ /index.php$is_args$args;
after that run
sudo service nginx restart
Try to put the root and index directives in the server block (outside of the location block), and modify the try files to: try_files $uri $uri/ /index.php?$query_string;
Also you have the server_name two times on the 3rd line.
You can find a comprehensive guide here: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04
I am trying to run Wordpress on my NGinx server, however I think that something is wrong because the index.php file keeps downloading rather than being run.
here is my sites-available:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
server_name your_domain.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I checked to make sure that I had php7.1-fpm running and that my nginx was running as well. Both checked out to be just fine.
What am I missing?
Thanks
This link solved my problem. I was missing some packages that wordpress needed. After going through that guide everything worked
I want to remove php file extension. So I searched and tried this code.
It works well but when I tried un-existed page.
it just said "File not found". before I write 'try_files $uri ...;' It return /404.html.
I tried try_files $uri =404; in .php$ but all pages return to /404.html.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /var/www/html;
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
error_page 404 /404.html;
location = /40x.html {
root /var/www/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
}
How Can I fix this??
You are passing .php files to the PHP processor without checking for existence.
You should modify your location ~ \.php$ block so that nginx handles non-existent files with a 404 response, rather than letting PHP complain.
Try something like:
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
I have removed the extra root directive as it was just confusing, so that the block now inherits the correct value from the outer block. I have removed fastcgi_index which is not used in this case. I have ordered fastcgi_param below the include to avoid silently overwriting your fastcgi_param statements.
I'm trying to configure nginx to serve 2 different php scripts from 2 different location. The configuration is as follows.
I have a Laravel installation which resides in /home/hamed/laravel in which its public directory should be served.
I have a Wordpress installation in /home/hamed/www/blog.
And this is my nginx configuration:
server {
listen 443 ssl;
server_name example.com www.example.com;
#root /home/hamed/laravel/public;
index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location /blog {
root /home/hamed/www/blog;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
}
location / {
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The problem is when trying to access the wordpress section by calling example.com/blog still the the laravel installtion takes over the request.
Now I have tried replacing root directive inside location blocks with alias to no avail.
According to this guide having the index directive or try_files inside location triggers an internal redirect which I suspect causes this behavior.
Would someone please help me figure this out?
The problem is that location ~ \.php$ { ... } is responsible for handling all of your php scripts, which are divided across two different roots.
One approach is to use a common root for the server container and perform internal rewrites within each prefix location block. Something like:
location /blog {
rewrite ^(.*\.php)$ /www$1 last;
...
}
location / {
rewrite ^(.*\.php)$ /laravel/public$1 last;
...
}
location ~ \.php$ {
internal;
root /home/hamed;
...
}
The above should work (but I have not tested it with your scenario).
The second approach is to use nested location blocks. The location ~ \.php$ { ... } block is then replicated in each application's location block. Something like:
location /blog {
root /home/hamed/www;
...
location ~ \.php$ {
...
}
}
location / {
root /home/hamed/laravel/public;
...
location ~ \.php$ {
...
}
}
Now that one has been tested to work.
Thanks to #RichardSmith I finally managed to create the right configuration. Here is the final working config. I had to use the combination of nested location blocks and an inverse regex match for it to work.
server {
listen 443 ssl;
server_name example.com;
root /home/hamed/laravel/public;
# index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location ~ ^/blog(.*)$ {
index index.php;
root /home/hamed/www/;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog`
index index.php;
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I followed a guide from here to install wordpress on my ubuntu server. Upon entering the url http://mydomain.com loads me the front page.
server {
listen 80 default_server;
root /var/www/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.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;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I want to load wordpress from my subdomain i.e., http://mydomain.com/wordpress. I went on to modify the location but it can't load properly (it still looks for files under http://mydomain.com).
root /var/www;
index index.php index.html index.htm;
location /wordpress {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
or
location /{
try_files $uri $uri/ /wordpress/index.php?q=$uri&$args;
}
How do I fix this? thanks
Your wordpress looking for his files in root directory. /
So you need to tell him, to search his files in the right directory. /wordpress/
There are two ways.
Better: to change the path using wordpress config file for rewriting resources path
from
/css/*, /* etc
to
/wordpress/css/*, /wordpress/* and so on.
Worst: to redirect your wordpress resources to root directory. Something like this:
location ~ ^/images/(.*)$ {
try_files $uri /wordpress/images/$uri =404;
access_log off;
}