In my configuration i use the host-name($host) as directory so i don't need to make a entry for each domain in the config (only for development purpose)
one issue is that try_files failed i tried different ways but all of them without success.
try_files html\$host$uri =404;
try_files $document_root\$host$uri =404;
try_files $uri =404;
try_files $document_root\$host$fastcgi_script_name =404;
server {
listen 80;
server_name default;
charset UTF-8;
#access_log logs/host.access.log main;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root html/default;
}
location / {
root html/$host;
index index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#try_files html\$host$uri =404;
#try_files $document_root\$host$uri =404;
#try_files $uri =404;
#try_files $document_root\$host$fastcgi_script_name =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root\$host$fastcgi_script_name;
include fastcgi_params;
}
The concept it self is correct, but you are applying it in the wrong place, you should place this as a root for the server, not in the try_files
Also I believe you should use $http_host not $host, refer to this answer to know the difference
I don't know why your paths are not absolute, they should start with /
I'll add changes to your current posted config
server {
#since you will match multiple domains I removed the server_name
listen 80 default_server;
charset UTF-8; # don't really need this but it won't hurt.
root /usr/share/nginx/html/$http_host; #assuming this path
index index.php index.html index.htm;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root /usr/share/nginx/html/default;
}
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
# these are the minimal required parameters.
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
Related
I don't understand why nginx cannot find files when the root is under home directory, e.g. /home/ubuntu/tabs. It will give 404 error.
It works fine when the root is /var/www/tabs.
server{
listen 80;
#root /var/www/tabs;
root /home/ubuntu/tabs;
server_name 18.191.229.199;
index main.html ;
location / {
try_files $uri $uri/ =404;
}
location /folder {
try_files /folder/main1.html /folder/main2.html =404;
}
}
I'm having problem with configuring my nginx.conf file to run django server on main domain and a WordPress site on domain.com/blog.
This is my configuration file which my WordPress dir is
/var/www/varzesh-kon/blog/:
upstream Main_Project_server {
server unix:/home/amirfarsad/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 2n9l.s.serverhost.name;
client_max_body_size 4G;
access_log /home/amirfarsad/logs/nginx-access.log;
error_log /home/amirfarsad/logs/nginx-error.log;
location /static/ {
alias /home/amirfarsad/Main_Project/static/;
}
location /media/ {
alias /home/amirfarsad/Main_Project/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://Main_Project_server;
break;
}
}
location /blog/ {
root /var/www/varzesh-kon/blog/;
index index.php index.html index.htm;
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
root /var/www/varzesh-kon/blog/;
}
}
My django site works well but when I go to domain.com/blog, it gives me a 404 not found nginx page.
Try changing your bottom location block
location ^~ /blog {
root /var/www/varzesh-kon/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
Explanation:
In case of the root directive, full path is appended to the root including the location part
In your case:
location /blog/ {
root /var/www/varzesh-kon/blog/;
The final path that nginx will derive is going to be:
/var/www/varzesh-kon/blog/blog
That's why its showing you 404 not found page
Solution:
Either use alias instead of root
or change root path to /var/www/varzesh-kon/
location /blog/ {
root /var/www/varzesh-kon/;
...
}
location /blog/ {
alias /var/www/varzesh-kon/blog/;
...
}
for more: wiki
What I am working with :
Instance - amazon EC2,
OS - linux AMI,
Web-server - nginx
I have installed phpmyadmin according to what is mentioned in the AWS documentation here
I have mysql up and running, as well as php-fpm and nginx. I also created a symlink between the directory where phpMyAdmin i.e /var/www/html/phpMyAdmin -> /usr/share/nginx/www/html
below is what I have in my nginx.conf file
server{
listen 80;
server_name localhost;
root /var/www/html/phpMyAdmin;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#location ^~ /phpMyAdmin/ {
# root /var/www/html/phpMyAdmin;
# index index.php;
# include fastcgi_params;
# fastcgi_pass unix:/var/run/php-fpm.sock;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME script/$fastcgi_script_name;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html/phpMyAdmin;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
But none of this seems to work. I am trying from hours but unable to figure out what may be the problem due to which its not working. any help would be really great. Thanks !
You can try with below vhost config file. Error page redirection handling appears a problem with your config file.
server {
listen 80;
root /var/www/html/phpMyAdmin; # Change the PHPMyAdmin location
index index.php;
server_name default_server;# Set server name based on sub-domain
access_log /var/log/nginx/phpMyAdmin-access.log;
error_log /var/log/nginx/phpMyAdmin-error.log notice;
charset utf-8;
error_page 404 403 500 502 503 504 /index.php;
location / {
try_files $uri $uri/ /index.php?$args;
access_log off;
expires max;
}
location ~* .(jpg|jpeg|png|gif|ico|css|js|ico|swf)$ { expires 365d; access_log off; log_not_found off;}
location = /favicon.ico { log_not_found off; access_log off; allow all; }
location = /robots.txt { access_log off; log_not_found off; allow all; }
location ~ \.php$ {
expires off;
fastcgi_read_timeout 600;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/phpmyadmin.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I managed to make my index.php script to run properly when I get a url like:
http://myserver.com/?something.m3u8
Still, I need to remove the ? from it.
Any ideas?
My Nginx config looks like:
server {
listen 6868 default_server;
root /var/tmp/mfl;
index index.php;
# Make site accessible from http://localhost/
server_name localhost;
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;
}
Apparently is was an one line solution
location / {
try_files $uri $uri/ /index.php?$args; #appended this line
}
I have an emberjs app and using nginx to serve it. I want to redirect to index page if the file not found. I tried using the following configuration but its showing blank page instead of index page.
server
{
listen 80 ;
server_name x.x.x.x
xyz.example.com;
root /home/ubuntu/website/dist/;
index index.html index.htm;
error_page 404 /home/ubuntu/website/dist/;
location / {
try_files $uri $uri.html $uri/ /fallback/index.html;
}
location /fallback {
root /home/ubuntu/website/dist;
}
}
I added the following block after the reference
location /{
error_page 404 = #foobar;
}
location #foobar {
rewrite .* / permanent;
}
Finally, the conf file look like this
server
{
listen 80 ;
server_name X.X.X.X
xyz.example.com;
root /home/ubuntu/website/dist/;
index index.html index.htm;
error_page 404 = #foobar;
location /{
try_files $uri $uri.html $uri/ =404;
}
location #foobar {
rewrite .* / permanent;
}
}
After update of OP
location /{
error_page 404 = #foobar;
}
Should be
location /{
try_files $uri $uri.html $uri/ #foobar;
}