Nginx cannot find ~/public_html/info.php
I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:
- Ubuntu 18.04 LTS 64-bit.
- Nginx (don't know how to check version)
- PHP 7.2
- The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)
- A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.
/etc/nginx/site-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
}
}
Please help.
php nginx
add a comment |
I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:
- Ubuntu 18.04 LTS 64-bit.
- Nginx (don't know how to check version)
- PHP 7.2
- The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)
- A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.
/etc/nginx/site-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
}
}
Please help.
php nginx
add a comment |
I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:
- Ubuntu 18.04 LTS 64-bit.
- Nginx (don't know how to check version)
- PHP 7.2
- The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)
- A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.
/etc/nginx/site-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
}
}
Please help.
php nginx
I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:
- Ubuntu 18.04 LTS 64-bit.
- Nginx (don't know how to check version)
- PHP 7.2
- The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)
- A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.
/etc/nginx/site-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
}
}
Please help.
php nginx
php nginx
asked Feb 7 at 13:50
phpNewbiephpNewbie
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have PHP files in two roots, so you need two location
blocks to process those URIs. The simplest solution is to use a nested location
block.
For example:
location / {
try_files $uri $uri/ =404;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
location ~ .php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ .php$ {
...
}
Place the outer location ~ .php$
block below, otherwise it will match all PHP URIs first. See this document for details.
Use named captures, as numeric captures will be out of scope in the nested location
block.
I don't know what's in your snippets file, but you probably want to avoid try_files
(because of this issue with alias
) and you need to use $request_filename
to locate the path to the SCRIPT_FILENAME
.
The if
block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if
.
Surely thatif
could be replaced withtry_files $url =404
or something similar?
– grawity
Feb 7 at 15:18
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1403134%2fnginx-cannot-find-public-html-info-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have PHP files in two roots, so you need two location
blocks to process those URIs. The simplest solution is to use a nested location
block.
For example:
location / {
try_files $uri $uri/ =404;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
location ~ .php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ .php$ {
...
}
Place the outer location ~ .php$
block below, otherwise it will match all PHP URIs first. See this document for details.
Use named captures, as numeric captures will be out of scope in the nested location
block.
I don't know what's in your snippets file, but you probably want to avoid try_files
(because of this issue with alias
) and you need to use $request_filename
to locate the path to the SCRIPT_FILENAME
.
The if
block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if
.
Surely thatif
could be replaced withtry_files $url =404
or something similar?
– grawity
Feb 7 at 15:18
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
add a comment |
You have PHP files in two roots, so you need two location
blocks to process those URIs. The simplest solution is to use a nested location
block.
For example:
location / {
try_files $uri $uri/ =404;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
location ~ .php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ .php$ {
...
}
Place the outer location ~ .php$
block below, otherwise it will match all PHP URIs first. See this document for details.
Use named captures, as numeric captures will be out of scope in the nested location
block.
I don't know what's in your snippets file, but you probably want to avoid try_files
(because of this issue with alias
) and you need to use $request_filename
to locate the path to the SCRIPT_FILENAME
.
The if
block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if
.
Surely thatif
could be replaced withtry_files $url =404
or something similar?
– grawity
Feb 7 at 15:18
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
add a comment |
You have PHP files in two roots, so you need two location
blocks to process those URIs. The simplest solution is to use a nested location
block.
For example:
location / {
try_files $uri $uri/ =404;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
location ~ .php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ .php$ {
...
}
Place the outer location ~ .php$
block below, otherwise it will match all PHP URIs first. See this document for details.
Use named captures, as numeric captures will be out of scope in the nested location
block.
I don't know what's in your snippets file, but you probably want to avoid try_files
(because of this issue with alias
) and you need to use $request_filename
to locate the path to the SCRIPT_FILENAME
.
The if
block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if
.
You have PHP files in two roots, so you need two location
blocks to process those URIs. The simplest solution is to use a nested location
block.
For example:
location / {
try_files $uri $uri/ =404;
}
location ~ /.ht {
deny all;
}
location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
location ~ .php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ .php$ {
...
}
Place the outer location ~ .php$
block below, otherwise it will match all PHP URIs first. See this document for details.
Use named captures, as numeric captures will be out of scope in the nested location
block.
I don't know what's in your snippets file, but you probably want to avoid try_files
(because of this issue with alias
) and you need to use $request_filename
to locate the path to the SCRIPT_FILENAME
.
The if
block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if
.
answered Feb 7 at 14:43
Richard SmithRichard Smith
513148
513148
Surely thatif
could be replaced withtry_files $url =404
or something similar?
– grawity
Feb 7 at 15:18
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
add a comment |
Surely thatif
could be replaced withtry_files $url =404
or something similar?
– grawity
Feb 7 at 15:18
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
Surely that
if
could be replaced with try_files $url =404
or something similar?– grawity
Feb 7 at 15:18
Surely that
if
could be replaced with try_files $url =404
or something similar?– grawity
Feb 7 at 15:18
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.
– Richard Smith
Feb 7 at 15:28
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1403134%2fnginx-cannot-find-public-html-info-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown