I installed October CMS on my Nginx server, installed fine. Front end works with index.php and the style is correct. However backend does not. It just gives me a 404 page error. I've followed everything on the docs correctly, tried different sites-avaliable config files and they don't seem to work but give a 502, which I've checked the logs too and nothing in there.
This is my one3.com config file:
server {
listen 80;
root /storage/www/one3community.com;
index index.php index.html index.htm;
listen 443;
ssl on;
ssl_certificate /ssl_keys/one3community.com/public.pem;
ssl_certificate_key /ssl_keys/one3community.com/private.pem;
# Make site accessible from http://localhost/
server_name www.one3community.com;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Converted default apache .htaccess file to Nginx. Loaded into sites-available .conf file restarted nginx. Now up and running.
** Had to replace every break with last in nginx file otherwise causes file download **
Related
I have an existing Nginx config file that I've configured to work with a docker container that runs php. Below is the nginx config file:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Now I wanted to test a certain project in the nginx configuration, the project requires that I use its own configuration of nginx
# nginx configuration
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?path=$1;
}
}
I am getting difficulties on adding the new configurations onto the already existing Nginx file that connects to the PHP container. I need some help on how I can add the new configurations without compromising the old Nginx file because the routes wont be redirected to the php container if I use the Nginx file that comes with the project.
I'm trying to change from plain http://sitename/?p=123 to http://sitename/postname with rewrite rule but it gives error 404 Not found. This is my nginx server block code:
server {
listen 80;
listen 443 ssl;
root /var/www/sitename/html;
index index.php index.html index.htm;
server_name sitename www.sitename;
client_max_body_size 10M;
# Certificates handled by CertBot
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
server_name sitename www.sitename;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ /index.php$is_args$args;
# rewrite ^ http://$server_name$request_uri permanent;
rewrite ^/(.*)$ http://$server_name/$1 permanent;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# root /data/www;
# index index.html index.htm;
}
}
I want to use rewrite so I redirect https of front-end to http and wp-admin and wp-login as https which is handled by Wordpress.
How can I fix it?
Created a separate similar server block with 443 listen. Included the cert lines and removed the location from 443 block.
Currently, i have nginx/php-fpm setup in a docker container and the wordpress folder mounted as a volume in the container. I want xyz.com/blog and xyz.com/blog/ both function exactly the same.
I have the following nginx configuration:
server {
listen 80 default_server;
server_name 0.0.0.0;
root /mnt/blog;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php last; #converted from .htaccess
}
}
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
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;
}
}
While hitting xyz.com/blog/ - the wordpress site gets loaded as expected but when i remove the trailing slash(/) - xyz.com/blog, i get 301 Moved Permanently error. How can i ensure that that both xyz.com/blog and xyz.com/blog/ behave correctly and load the wordpress site?
You want /blog to invoke /index.php without first redirecting to /blog/.
Based on your existing solution you could change the if (!-e ... to if (!-f .... See this document for details.
However, the same functionality can be achieved with:
location / {
try_files $uri /index.php;
}
See this document for details.
I've been attempting to upgrade to php 7.1 using phpbrew, and elected to install it with nginx, as I read everywhere that it was simpler than Apache (not that simple, in my humble opinion).
As I was trying to run Symfony2 with nginx, I came across this doc page, which gives a basic config for Sf2 on nginx.
I managed to configure php-fpm to serve app_dev.php, and every file ending in .php correctly. However, as soon as I go to a different URL (/home for instance), the nginx config breaks and I get a File not found error in php-fpm.
How do I configure the nginx virtual host to allow for everything after app_dev.php or app.php to be rewritten (as it would with modrewrite on apache2)?
My nginx file for reference:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location /my-app {
index web/app_dev.php;
try_files $uri /web/app.php$is_args$args;
}
location /dist {
root /usr/share/nginx/html;
index depp/index.php;
try_files $uri /depp/index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REQUEST_URI $uri?$args;
}
}
You are missing a rewrite condition to catch-all the incoming requests and forward them to your front controller.
Try something like:
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location /my-app {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# Symfony 2 app index
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/home/gabriel/.phpbrew/php/php-7.1.0/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to any other php files
location ~^.*\.php(/|$) {
deny all;
}
You current configuration is a more general configuration for any .php script but Symfony2 and framework in general only provide a catch-all front-controller.
Currently I have this piece of code:
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
This works OK for the following:
example.com/foo redirects to index.php
However example.com/foo?bar doesn't work. How do you make it work?
FWIW: I don't experience this problem in Apache's mod_rewrite equivalent. Basically, I moved a site that works from Apache to Nginx. Now I experience this issue.
Edit:
To be clear here's what I indent to do:
example.com/foo
example.com/foo/bar/etc
example.com/foo?bar
example.com/foo?bar=quz
Should all serve index.php "silently" without changing the URL of the browser's address bar.
I just tested it with the following config, and I believe this does want you want:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/www/test;
index index.php index.html index.htm;
# Make site accessible from
server_name test.myhost.nl;
location / {
# First attempt to serve request as file, then as directory, then fall back to index.php
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri = 404;
# Fix for server variables that behave differently under nginx/php-fpm than typically expected
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with ngingx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Pass to upstream PHP-FPM; This must match whater you name your upstream connection
#fastcgi_pass phpfpm;
fastcgi_pass 127.0.0.1:9000;
}
}