ON nginx I configured this role :
On server file:
location ~ .php$ {
root /var/www/public;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# On Domain file
location /support {
try_files $uri $uri/ /index.php;
autoindex on;
autoindex_exact_size off;
}
#for admin panel
location ^~ /support/admin {
try_files $uri $uri/ /admin/index.php;
autoindex on;
autoindex_exact_size off;
}
I solved 403 error problem when I added autoindex on; line,
but I see "index of support" message when I Click on my Domain
and when I want to Click on index.html file, this file not to show, and instead of it, download is done.
How can I resolve this problem and What is my problem in my shared code
Try this
location /support {
default_type "text/html";
types { application/octet-stream html; }
…
}
Related
I need to setup two projects under one domain(react+symfony), here is my nginx config:
server {
listen 80;
server_name domain.ltd;
rewrite_log on;
root /var/www/frontend;
access_log /var/log/nginx/project_access.log;
error_log /var/log/nginx/project_error.log;
location / {
index /index.html;
}
location /api/ {
alias /var/www/backend;
try_files $uri /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
}
}
root / works fine so static index.html I'm getting okay without any issues. but symfony application for /api throws an error(403 Forbidden) in nginx error.log:
32349 open() "/var/www/frontend/index.php"
For some reason alias ignored, what i'm doing wrong?
Thanks in advance
The location and the alias values should both end with a / or neither end with a /. The last parameter of the try_files statement is a URI, so you need to include the /api/ prefix.
For example:
location /api/ {
alias /var/www/backend/;
try_files $uri /api/index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
}
Also, add a try_files statement to the nested location block to avoid passing uncontrolled requests to PHP. The correct value for SCRIPT_FILENAME is $request_filename which works with either root or alias.
I'm having trouble setting up two locations in my Nginx conf file.
I had no problem having two locations before adding one with an alias.
Location / with alias doesn't work.
Location /dev without alias works.
I would like to use two aliases because I have two folders : prod and dev.
Here is my current conf :
server {
listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
root /home/domain/public_html/www/;
index index.html index.htm index.php index2.php;
location / {
alias /home/domain/public_html/www/prod/;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /dev {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\.ht {
deny all;
}
}
What is happening is that accessing domain.com/dev/ works great but as soon as it's on the / location, I get a "no input file specified" error.
If I enter domain.com/license.txt, I can see Wordpress's license file.
If I try domain.com/index.php, I get the error.
I'm already using $request_filename to avoid root vs alias problems, any idea ?
You do not need to use alias in this scheme, but if you wish to run PHP with two separate roots, you will need to use a nested location block.
For example:
root /home/domain/public_html/www/prod;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /dev {
root /home/domain/public_html/www;
try_files $uri $uri/ /dev/index.php?q=$uri&$args;
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
The first two location blocks are your /prod/ configuration, with the correct root to resolve URIs like /index.php.
The last location and nested location blocks are your /dev/ configuration. The root is set to the correct value to resolve URIs like /dev/index.php.
See this document for more.
So here's my server block
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /phpmyadmin {
alias /var/www/phpmyadmin;
}
location /nginx_status {
stub_status on;
access_log off;
}
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;
}
}
browsing http://domain.tld/index.php works fine the only problem im encountering is browsing http://domain.tld/phpmyadmin/. It returns 404 yet the folder /var/www/phpmyadmin exist on the server. Viewing /var/log/nginx/error.log, no error is being logged there yet the access to it is logged in /var/log/nginx/access.log. What could be the problem here?
The problem is that phpmyadmin is a PHP application and your location ~ \.php$ block does not point to the correct document root.
You need to construct two PHP locations with different document roots.
If phpmyadmin is located at /var/www/phpmyadmin, you do not need an alias directive, as a root directive will be more efficient. See this document.
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /nginx_status {
stub_status on;
access_log off;
}
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;
}
location ^~ /phpmyadmin {
root /var/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;
}
}
}
The location ^~ /phpmyadmin is a prefix location that takes precedence over the regex location normally used to process .php files. It contains a location ~ \.php$ block which inherits a value of /var/www for the document root.
It is advisable to include fastcgi_params before defining other fastcgi_param parameters otherwise your custom values may be silently overwritten.
See this document for more.
I currently have a WordPress site, which is currently installed on a subdirectory. However, there seems to be an issue.
The site appears as it supposed to, but the posts are either having a 404 error page and redirecting to the root of the site. Here is the screenshot of the setup I currently have on the server:
If anyone can please help me out here. I would appreciate it.
UPDATE (29/04/2018):
I have been working on this and it has come to a point where the blog page opens however, the posts are coming up with a 404 error message from the ROOT site.
For example:
When opening the subdirectory ( example.com/blog ), the site will appear. When clicking on a post, the 404 page will be displayed, but on the example.com site.
This makes me believe that there is an issue with the 2 WordPress installations however, I cannot be certain.
Here are my Nginx configurations for the root and the blog:
location /blog {
root /var/www/html/aus;
index index.php index.html;
access_log /var/log/nginx/blog.access.log;
error_log /var/log/nginx/blog.error.log;
try_files $uri $uri/ /blog/index.php$is_args$args;
if (!-e $request_filename) {
rewrite ^.*$ /blog/index.php last;
}
}
location / {
access_log /var/log/nginx/root.access.log;
error_log /var/log/nginx/root.error.log;
#try_files $uri $uri/ =404;
gzip off;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
reset_timedout_connection on;
include fastcgi.conf;
fastcgi_param SERVER_PORT 80;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
If you are getting 404 error page or some other error for posts, reset your permalinks setting.
Go to Settings → Permalinks and reset it to default settings and save it.
Your nginx config should be something like this:
upstream php-upstream {
server unix:/var/run/php5-fpm.sock fail_timeout=0;
}
...
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
reset_timedout_connection on;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_param SERVER_PORT 80;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-upstream;
fastcgi_index index.php;
}
...
That's an excerpt from a working nginx config for a wordpress website. Of course you should adapt it and modify it accordingly to suit your needs.
So here's my server block
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /phpmyadmin {
alias /var/www/phpmyadmin;
}
location /nginx_status {
stub_status on;
access_log off;
}
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;
}
}
browsing http://domain.tld/index.php works fine the only problem im encountering is browsing http://domain.tld/phpmyadmin/. It returns 404 yet the folder /var/www/phpmyadmin exist on the server. Viewing /var/log/nginx/error.log, no error is being logged there yet the access to it is logged in /var/log/nginx/access.log. What could be the problem here?
The problem is that phpmyadmin is a PHP application and your location ~ \.php$ block does not point to the correct document root.
You need to construct two PHP locations with different document roots.
If phpmyadmin is located at /var/www/phpmyadmin, you do not need an alias directive, as a root directive will be more efficient. See this document.
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /nginx_status {
stub_status on;
access_log off;
}
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;
}
location ^~ /phpmyadmin {
root /var/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;
}
}
}
The location ^~ /phpmyadmin is a prefix location that takes precedence over the regex location normally used to process .php files. It contains a location ~ \.php$ block which inherits a value of /var/www for the document root.
It is advisable to include fastcgi_params before defining other fastcgi_param parameters otherwise your custom values may be silently overwritten.
See this document for more.