Nginx sites-available doesn't work - nginx

I've got this really simple server block under sites-available.
Problem: When I try to access to mydomain.com, Nginx returns a « 404 Not Found », but if I try to access to a file in particular, it works fine, like mydomain.com/index.php
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location / {
try_files $uri =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Note that:
my hosts file is configured ;
I restart Nginx after each edit ;
the access rights, user and group are correct ;
the error.log file is empty, the access.log returns me all the 404 ;
I tried to change the config by adding/removing some lines, still no changes ;
the site is enabled in sites-enabled with a correct symlink (I tried to edit it and it opened the right file) ;
I've got a few sites on the same server who runs well (so the including of sites-available and sites-enabled is OK, and Nginx works fine).

So, the answer was giver to me on ServerFault by Alexey Ten, here is a copy of the answer
Your try_files directive is too restrictive and, I guess, is in wrong place.
Either remove location / completely, it doesn't makes much sense, or, at least add $uri/ so index directive will work.
try_files $uri $uri/ =404;
But my guess is, you need to move this try_files into location ~ \.php$, this will make sure that php-file exsists before pass it to PHP-FPM for processing. All other files will be served by nginx with proper use of index directive.
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

Related

nginx sites available redirecting badly

I have a server with multiple sites running on nginx. I'm trying to add another one, and somehow I keep getting redirected to the wrong path.
What I usually do is copy from a working one and change the domain and paths, then create de symlink. I've also checked that they both have the same permissions, so I'm not seeing any mistake on my part. all paths work and of course I've restarted nginx. I also restarted the whole server out of desperation.
The failing one
server {
listen 80;
root /data/alvarezarango.com/www;
index index.php index.html index.htm;
server_name alvarezarango.com www.alvarezarango.com;
error_log /data/alvarezarango.com/logs/error.log error;
access_log /data/alvarezarango.com/logs/access.log;
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
the working one:
server {
listen 80;
root /data/spiraxtime.com/www;
index index.php index.html index.htm;
server_name spiraxtime.com www.spiraxtime.com;
error_log /data/spiraxtime.com/logs/error.log error;
access_log /data/spiraxtime.com/logs/access.log;
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I get redirected to my main domain, but i can't see why. Is anything I can check to understand the redirects?

nginx multiple domain issue

I want to host two website on my ubuntu VPS with nginx . so i created two copy of default config :domain1.com and domain2.com
config for domain1.com:
server {
listen 80 ;
listen [::]:80 ;
root /home/sher/ftp/www/public;
server_name domain1.com www.domain1.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
if user type in www.domain1.com in browser he would see domain1 site. but if he hit it without www he would see domain2.com .i've created symlinks from these two config files to sites-enabled .
P.S : I set listen to default_server for domain1 and it seems the issue solved but im not sure if it is the right way.

NGINX and Clickonce

I am trying to get my NGINX website to work with Microsoft clickonce. I have the web site able to display the standard microsoft clickonce install page, but when I get to the Install page on my website and click the install button, nothing happens. I believe at that point it is supposed to download an EXE, which it doesn't do. Does anyone have any experience in getting clickonce to work with NGINX and what is required? Any information about setting correct directives, location, configuration, mime types or whatever else may be required.
Here is my current config file. I am new to NGINX, so perhaps it is something I am doing wrong with the location, or...?
server {
listen 80;
root /usr/share/nginx/www/MyWebSite;
index index.php index.html index.htm;
server_name MyWebSite.com www.MyWebSite.com;
client_max_body_size 100M;
access_log /var/log/nginx/MyWebSite.access.log;
error_log /var/log/nginx/MyWebSite.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /Application/MyApp/ {
try_files /Application/MyApp/publish.htm /Application/MyApp/setup.exe /application/MyApp/MyApp.application;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
types {
application/x-ms-application .application;
}
}

Wordpress and NGINX /wp-admin redirect loop

My nginx.conf has a server blog containing this:
location ~ \.php$ {
root /var/www/html/blog;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /blog {
root /var/www/html/blog;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
But with these settings when I try to access /blog/wp-admin my browser gets stuck in some redirect loop.
If I change the root URLs in nginx.conf to /var/www/html, /blog/wp-admin works, but my post permalinks give me a 404 error.
My WP files are located in /var/www/html/blog. I have 'SSL Insecure Content Fixer' plugin installed because my images giving a mixed content error on my site, which has a Cloudflare page rule to always use SSL.
My WP address and WP home are both set to http://xxx/blog.
Anybody fixed something similar?
Thanks
I think that the main problem is an inconsistency with your root directive. Your PHP configuration has WordPress in /var/www/html/blog whereas your static configuration has WordPress in /var/www/html/blog/blog.
Assuming that WordPress is installed in the root of /var/www/html/blog and that the URIs should be prefixed with /blog/ for both real files and permalinks, the correct URI for the entry point should be /blog/index.php.
The nginx.conf file should probably be:
root /var/www/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
location /blog {
include /etc/nginx/mime.types;
try_files $uri $uri/ /blog/index.php;
}
If you have a conflicting root directive within the outer server container, the above root directive could be placed inside the two location blocks unmodified.
I would try /blog/index.php rather than /blog/index.php?q=$uri&$args as the last element of try_files because in my experience, WordPress uses the REQUEST_URI parameter to route permalinks rather than the q argument as you have implied, but YMMV.
If you do have other applications in this servers root and would like to segregate the WordPress root more completely, you might nest the PHP location block like this:
location ^~ /blog {
root /var/www/html;
include /etc/nginx/mime.types;
try_files $uri $uri/ /blog/index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}

NGINX doesn't work if the domain has an accent mark

I have bought a domain with an accent mark inside: something like www.èxample.com.
My problem is that even if I configured my nginx server block (virtual host) in a correct way, it doesn't match the directory that I made.
I tried to use the same configuration with a domain without an accent mark and everything work fine.
This is my server block file inside site-availables folder:
server {
listen 80;
root /var/www/example.com;
index index.php index.html index.html;
server_name *.èxample.com;
client_max_body_size 4G;
charset utf-8;
access_log /var/www/example.com/logs/nginx-access.log;
error_log /var/www/example.com/logs/nginx-error.log;
location / {
try_files $uri $uri/ /index.html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
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;
}
}
How can I solve this problem?
Thanks!

Resources