Multiple PHP based server blocks in nginx - 404 Error - php

I am pretty new in Nginx and I have a problem with it. I have two wordpress websites and I want to start them with Nginx on a Centos Linux. What I have done is:
creating sites-available and sites-enabled in the /etc/nginx/
Put my website configuration files (.conf) into sites-available
I created symbolic links for them using ln -s
My configuration files are as follow:
nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# include /etc/nginx/conf.d/*.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
}
mydomain1.com.conf
server {
listen 80;
server_name mydomain1.com www.mydomain1.com;
location / {
root /var/www/html/;
index index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
mydomain2.com.conf
server {
listen 80;
server_name mydomain2.com www.mydomain2.com;
location / {
try_files $uri $uri/ =404;
root /var/www/aramis;
index index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
/etc/php-fm.d/www.conf
[www]
listen = /var/run/php-fpm/php-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nobody
listen.group = nobody
;listen.mode = 0660
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
I also tried to find php-fpm.sock but I was not successful.
What I get when try to see my websites from browser is 404 Error.

You have no root directive for your PHP location block. If it is the same root for all files (both static and dynamic) then move the root directive to the server block and allow the same value to be inherited by both location blocks.
server {
...
root /var/www/aramis;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
...
}
}
See this document for more.

Your need resolve the redundance
mydomain1.com.conf
server {
listen 80;
root /var/www/html/; # Set as global
server_name mydomain1.com www.mydomain1.com;
index index.php # Set index as global
location / {
# index index.php; #Remove this line
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
myworkingfile.conf
server {
listen 80;
server_name crm.coderic.net;
root /var/www/atrk/crm.atrk.com.co/public;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Related

Nginx Config 2 Web Applications on one Server

Good Morning guys,
i am trying to create an working Nginx Config.
I have two web applications:
/app/web
/app/api
My URL should look like this:
10.X.X.XX => /app/web
10.X.X.XX/api => /app/api
My current config:
server {
listen 80 default_server;
index index.php index.html index.htm;
root /app/web;
location /api {
root /app/api;
}
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M;
}
Any suggestions?
You can host multiple site if you follow below configuration. This is a working code. You can modify it according to your need
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;
server_name 192.168.0.132;
# Default index pages
index index.php;
# Root for / shipment
root /var/www/msdsl/shipment/public;
# Handle main root / shipment
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}
# Handle restora project, just replicate this section for further projects app3, app4
# by just replacing restora with appropriate tag(project1,/project2/project 3)
location /restora {
# Root for this project
root /var/www/msdsl/restora/public;
# Rewrite $uri=/restora/xyz back to just $uri=/xyz
rewrite ^/restora/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
location /tposreport {
# Root for this project
root /var/www/msdsl/tposreport/public;
# Rewrite $uri=/tposreport/xyz back to just $uri=/xyz
rewrite ^/tposreport/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /restora/xyz.
# But we don't want to pass /restora/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# This allows laravel to see /restora/xyz as just /xyz in its router.
# So laravel route('/xyz') responds to /restora/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/tposreport(.*)$) {
set $newurl $1;
root /var/www/msdsl/tposreport/public;
}
if ($newurl ~ ^/restora(.*)$) {
set $newurl $1;
root /var/www/msdsl/restora/public;
}
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fpm sock which is installed on your machine like php7.2, php5.6
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# Here we are telling php fpm to use updated route that we've created to properly
# response to laravel routes.
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Deny .ht* access
location ~ /\.ht {
deny all;
}
}`
Just copy and paste the same config into a new file
Name it the same as the subdomain you want it to run on
Give the path to the folder
Add the new subdomain to the hosts file
Restart nginx
Create 2 more servers in nginx. The first for /api (listen en 8080 for example), the other for /web (on 8081). Your main serveur (on 80/443) is then a proxy on the others :
upstream backend_api{
server 127.0.0.1:8080;
}
upstream backend_web{
server 127.0.0.1:8081;
}
server {
listen 80;
server_name www.example.com example.com;
location /api{
include proxy_params;
proxy_pass http://backend_api;
}
location / {
include proxy_params;
proxy_pass http://backend_web;
}
}
server {
listen 8080 default_server;
index index.php index.html index.htm;
root /app/api;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M; }
server {
listen 8081 default_server;
index index.php index.html index.htm;
root /app/web;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M;
}

"403 Forbidden nginx/1.14.0 (Ubuntu) "

I have configured nginx with multiple locations, one for a laravel project and another for a native php project.
Laravel project is working perfectly, but the second location seems to give:
"403 Forbidden nginx/1.14.0 (Ubuntu) "
Here is my default file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html/washyapi/public;
try_files $uri $uri/ index.php$is_args$args;
index index.html index.htm index.php;
server_name 167.71.239.178;
location / {
try_files $uri $uri/ index.php$is_args$args;
try_files $uri $uri/ /index.php?$query_string;
#index index.php;
}
location /admin {
root /var/www/html/;
#autoindex on;
#autoindex_exact_size off;
index index.php;
#try_files $uri /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}
after few days of struggling, here what i have done to make it work.
Here's the working configuration to have two apps working, where one application exists in a subdirectory of another.
default file :
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/top/public;
index index.html index.htm index.php;
server_name _;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location #nested {
rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
don't forget to restart the nginx server :
service nginx restart

php nginx config request for this address in a way that will never complete only php not wordpress

I am the beginner for nginx
I am tryingt to do
www.mywebsite.com/index.php to show in the url like www.mywebsite.com/
and
www.mywebsite.com/index.php/user/login to show in the url like www.mywebsite.com/user/login
and I got the error as show in the picture.
Here is my nginx.config
#user nobody;
worker_processes 4;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.wcp6288.com;
root /var/www/wangou;
index index.html index.htmi index.php;
#charset koi8-r;
#access_log logs/host.access.log main;
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
# Remove from everywhere index.php
if ($request_uri ~* "^(.*/)index\.php(/?)(.*)") {
return 301 $1$3;
}
}
# Remove trailing slash.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# Clean Double Slashes
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) /$1 permanent;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /var/www/wangou/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
try_files $uri $uri/ =404;
}
}
}
Thanks you for your helps.
Should you remove
try_files $uri $uri/ =404;
In block:
location ~ \.php$ {
root /var/www/wangou/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
try_files $uri $uri/ =404;
}
Here is my config and it's worked.
server {
listen 80;
listen 443 ssl http2;
server_name test.dev;
root "/var/www/html/test/";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
error_log /var/log/nginx/test.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}

NGINX server configuration for Codeigniter

/etc/nginx/conf.d/default.conf
server{
listen 80;
listen [::]:80;
server_name 192.168.56.101 192.168.101.100 localhost;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/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;
}
location ~ /\.ht {
deny all;
}
}
my codeigniter folder is 'ci' which is located in /var/www/html/ci
what configuration do I need to work url rewriting?...
I didn't want to change the current document root (/var/www/html)
since my 'ci' folder is located at /var/www/html/ci.
So instead, I created a new location block in /etc/nginx/conf.d/default.conf:
server{
...
location /ci {
try_files $uri $uri/ /ci/index.php?/$request_uri;
}
...
}
Thanks to Mert Öksüz for suggesting to use try_files $uri $uri/ /ci/index.php?/$request_uri;.
This one also work:
location /ci {
try_files $uri $uri/ /ci/index.php?$query_string;
}
Change your root to root /var/www/html/ci
Change your try_files to try_files $uri $uri/ /index.php?/$request_uri;
Be sure your fpm path (unix:/var/run/php-fpm/php-fpm.sock;) is correct.
I faced same problem and modified a little bit nginx conf from this site https://gist.github.com/yidas/30a611449992b0fac173267951e5f17f
server {
listen 80;
# For https
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;
server_name sc.hr;
root /var/www/sc/hr/;
index index.php index.html index.htm;
# set expiration of assets to MAX for caching
#location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
# expires max;
# log_not_found off;
#}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
# Deny for accessing .htaccess files for Nginx
location ~ /\.ht {
deny all;
}
# Deny for accessing codes
location ~ ^/(application|system|tests)/ {
return 403;
}
}
This conf worked on my laradock nginx container.
This worked for me
location ~* \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi.conf;
}
In case someone looking for CI 4 nginx on ubuntu 18.04 configuration :
root /var/www/ci/public;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# this is not working for the first get argument :
# try_files $uri $uri/ /index.php?/$request_uri;
# use this :
try_files $uri $uri/ /index.php$is_args$args;
}

nginx rewrite rules codeigniter

i am using Winginx for using nginx php and mysql on windows.
i want run and config CodeIgniter 2.
Winginx structure:
d:\winginx|
nginx.exe
php5\php.exe
mysql\mysqld.exe
\home\localhost\public_html\codeigniter
base this (http://wiki.nginx.org/Codeigniter)
and this (http://www.farinspace.com/codeigniter-nginx-rewrite-rules/)
and this (Nginx rewrite rule for CodeIgniter)
i try coonfig ci but i just see codeigniter 404 page (no nginx 404 page)
my ci config.php is
$config['base_url'] = "";
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
..
nginx.config is
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid temp/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_max_body_size 55m;
#gzip on;
scgi_temp_path temp/uwsgi_temp 1 2;
uwsgi_temp_path temp/uwsgi_temp 1 2;
fastcgi_connect_timeout 1;
include codeigniter.conf;
}
i include codeigniter.conf in nginx.conf
codeigniter.conf is:
server {
listen 80;
server_name localhost;
root home/localhost/public_html/codeigniter:
autoindex on;
index index.php;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
location = /index.php{
fastcgi_pass localhost:9000;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}
i try 3 different coonfig ci but i just see codeigniter 404 page (no nginx 404 page).
http://localhost/codeigniter/welcome/index -> CI 404 page!
http://localhost/codeigniter/welcome -> CI 404 page!
http://localhost/codeigniter -> CI 404 page!
i guest the problem is created by the php file location in codeigniter.conf
please help me to config it !
I'd try this
server {
listen 80;
server_name localhost;
root home/localhost/public_html/codeigniter;
autoindex on;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location = /index.php {
fastcgi_pass localhost:9000;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location ~ \.php$ {
return 444;
}
}
Here's what I use for a production CI install
server
{
listen 80 default_server;
server_name www.site.com;
root /var/www;
index index.php;
# canonicalize codeigniter url end points
location ~ ^/index.php/(.*[^/])$ { return 301 $scheme://$host/$1/$is_args$args; }
location ~ ^/index.php/(.*)/$ { return 301 $scheme://$host/$1/$is_args$args; }
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I got it working the following way...
server {
listen 127.0.0.1:80;
server_name xxxx www.xxxx;
root home/xxxx/public_html;
autoindex on;
index index.php;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
location ~ \.php$ {
if (!-e $document_root$document_uri){
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}

Categories