I'd like to pass all requests that don't match a file to a script at /framework/root.php?path={whatever the request $uri was}.
This works nicely for all requests except for only /, for example http://localhost.
Here is my Nginx config:
PS: Only started using Nginx coming from Apache today.
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
try_files $uri $uri/ #root;
}
location #root {
rewrite ^(.*)$ /framework/root.php?path=$1;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
Oooo got it.
try_files $uri $uri/ #root; should be try_files $uri #root; without the $uri/ part because that matches the base / directory.
Related
I have a react website which I am serving using NGINX. I wanted to create a blog for the same. So I tried to use wordpress in a sub-directory.
`
server {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name domain.com;
location / {
try_files $uri $uri/ =404;
}
location ^~ /blog {
client_max_body_size 10m;
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^/wordpress(/.+\.php)(.*)$;
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
#ssl certificates here
}
`
After hours of reading docs, blogs and stack I got my homepage set up. However all my pages on the blog are returning 404. I am attaching my nginx config.
My directory structure is
/var/www/html/ : root folder for my react website
/var/www/html/blog : root folder for my wordpress ( no /wordpress subfolder present)
Add this lines
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/blog
}
So I have two issues that I am trying to do. First I am trying to understand NGINX and use it for redirects and everything as well; however, if I try to remove the extensions from the php / html files it just downloads the file. If I use another thing to just try (without the file extension try) it just gives me 404s. I am not sure what the issue is nor what I am doing wrong.
First I'll show my remove php file extension issue:
server {
listen 443;
server_name XXX.XX.XX.XX;
root /var/www/html;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
Here is my other code to fix the problem above (but just gives me 404s)
server {
listen 443;
server_name XXX.XX.XX.XX;
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_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
XXX.XX.XX.XX is my the servers ip.
PS: I have read that I need the bottom section first to get the top section working.
Sources include:
How to remove both .php and .html extensions from url using NGINX?
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04
Ty for your help.
UPDATES:
Kuldeep KD - 770 /var/www/html (not solution but thanks)
Jignesh Joisar - Trying port 80 (failed but per usual thanks)
Check for necessary permissions on /var/www/html
you can change the permissions using
chmod -R 770 /var/www/html/
also check for the owner of directory you may have to change that as well, depends on user your nginx is using.
chown -R user:group /var/www/html
try to this one in 80 port
server {
listen 80 default_server;
root /var/www/html/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name localhost;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
# 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;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
I have the following nginx config for multiple root (html/web is default, html/pma is additional route):
server {
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name website.com;
server_tokens off;
root /usr/share/nginx/html/web;
index index.php;
location / {
try_files $uri /index.php?$args;
}
location ^~ /pma {
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
So, by default /html/web/index.php is opened, however website.com/pma opens /html/pma/, where pma is PHPMyAdmin.
The problem is:
PHPMyAdmin authentification form redirects to index.php. Therefore, when I write my credentials, it redirects me to /html/web/index.php! But should /html/pma/index.php. Even log out from PHPMyAdmin redirects to /html/web/index.php!
Could anyone suggest a better way of configuration?
I can see two errors in your configuration.
You try_files statement should end with /index.php or =404, not both! See this document for details.
The nested location ~ \.php$ block will never be consulted. The outer location ~ \.php$ block take precedence. You can solve this problem by using the ^~ modifier on the surrounding prefix location. See this document for details. For example:
location ^~ /pma {
...
location ~ \.php$ {
...
}
}
location ~ \.php$ {
...
}
i want to create routing like a:
hxxp://127.0.0.1/ <-- default with default location /var/www/ without listning directories
hxxp://127.0.0.1/allegro/
How to do it?
If i go to hxxp://127.0.0.1/allegro/scripts/test.php i see a blank page. If i go to hxxp://127.0.0.1/ php scripts excutes normally and i see phpinfo()
My nginx config:
server {
listen 8000 default_server;
listen [::]:8000 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php7.0-fpm.sock;
}
location /allegro/ {
alias /var/www/allegro/;
autoindex on;
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/run/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
There are multiple issues with your current configuration. The most important is that the URI /allegro/scripts/test.php is not processed by the nested location block, because regular expression location blocks take precedence (unless a ^~ modifier is used).
location ^~ /allegro/ {
root /var/www;
# autoindex on;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php7.0-fpm.sock;
include fastcgi_params;
}
}
The ^~ modifier ensures that this prefix location takes precedence over regular expression locations at the same level. See this document for details.
Note that the root statement is preferred over an alias statement where applicable (see this document for details).
The fastcgi_split_path_info and fastcgi_index directives are unnecessary for location blocks which do not match URIs with path info.
You will need to decide whether include fastcgi_params or include snippets/fastcgi-php.conf is the more appropriate source for FastCGI parameters.
I have a perfect configuration which is nginx, php5-fpm, apc, varnish and mariadb. Everything works flawless except;
I am hosting a single web site, since my server resources are high and available, I want to host other web sites on the same server. When I try to add different websites into nginx the service simply does not restart.
here's my configuration file when everything works:
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
I don't want to use seperate files for different virtual hosts, I want to do everything in default file. But when I add another virtual host like below and save default file. nginx won't restart.
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
server {
listen 8080;
root /usr/share/nginx/domain2;
index index.php index.html index.htm;
server_name www.domain2.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please help me resolve this issue. I think something is conflicting but don't know what.
ok I found the solution just by investigating error log.
2014/08/19 21:55:07 [emerg] 5927#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
error log tells me to increase hash bucket size..
I edited nginx.conf and set the bucket size to 32 as advised in error log, it didn't work at first, but then I set it to 64 and it worked.
just search for "bucket" in nginx.conf, uncomment it, then set to 64 (or above in some cases) it will work, unless there is another issue.