Add domain to Nginx config - nginx

here is my problem
I have domain "example.com" and i wanna that domain to be associated with folder /var/www/html/example.com
I've create file with name "example.com" in /etc/nginx/sites-available
server {
server_name http://example.com;
root /var/www/html/example.com;
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
And here is my default file on /etc/nginx/sites-enabled
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name 188.226.145.195;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /example.com/ {
try_files $uri $uri/ /example.com/index.php;
}
I know the problem is in that root /var/www/html but cant resolve it
Error log:
2016/02/27 13:53:53 [error] 10139#0: *11 directory index of "/var/www/html/" is forbidden, client: xxx.xx.xx.xx, server: 188.226.145.195, request: "GET / HTTP/1.1", host: "www.example.com"
Hope this is enough information so some1 can help.
Thanks.

I did it the problem was that i didn't give root to default location, when i do that works like a charm : )

Related

nginx config does not take action

I'm trying to setup a basic nginx server. Usually I'm not working with nginx and I ran in to some issues I'm unable to wrap my head around.
I have a debain server with all the necessary things installed (like php, mariadb, ufw,...) and I want to run my website somewhat like this:
xxx.xxx.xxx.xxx -> /var/www/
http(s)://(www).lechner.io -> /var/www/domains/lechnerio/
I want both https and http and the domain with www and without pointing to the folder /var/www/domains/lechnerio and the IP Address pointing to /var/www/
First things first, only getting :80 working.
I have the following config setup:
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
however, when I now try to visit the IP the nginx welcome site is shown. When I access it via domain, it also shows the files from /var/www/ even though i reloaded everything. nginx -t is working. A link from /etc/nginx/sites-available/lechnerio to /etc/nginx/sites-enabled/
Any input very welcome!
try following
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io www.lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.html index.htm;
server_name _; # everything else
location / {
try_files $uri $uri/ =404;
}
}

codeigniter and wordpress install with nginx but blog internal URL is not working

I have installed the code igniter project on the root directory and WordPress blog in the /blog directory. But the internal pages of blogs are not working. Below is the default file code. Tried all the solution given at the community but it is not working. //example.com/blog/sample-post/ this is not working. But example.com/blog/?p=2 this will work. Any help will be much appreciated
server {
listen 80 default_server;
server_name www.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /home/ssl/pte.crt;
ssl_certificate_key /home/ssl/pte.key;
root /var/www/html;
server_name www.mydomain.com;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
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/?q=$uri&$args;
}
location /blog/ {
try_files $uri $uri/ /blog/index.php
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# allow all;
#}
}

Default redirect in the subfolder's root is not redirected to index.php

In the root directory, any redirect to localhost/ is simply redirected to index.php, but if you go inside the localhost/phone subdirectory, there's an index.php page too, and as you type localhost/phone/ you are not redirected to the index.php page. No idea why. This is my server configuration.
server {
listen 80;
listen [::]:80 default_server;
server_name localhost;
charset UTF-8;
index index.html index.php;
location / {
root xyz;
proxy_set_header Connection "";
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
rewrite ^/(.*)/$ /$1 permanent;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|webp)$ {
expires 365d;
}
}
I tried to point the directory and added defaults for index.php and it works, but now all the url's inside /phone/ are showing blank.
location /phone {
index index.html index.php;
}
By "showing blank" means that they show a 404 error according to the error log
2019/05/08 23:40:58 [error] 26240#14076: *1 CreateFile()
"C:\xyz\nginx/xyz/phone/index" failed (2: The system cannot find
the file specified), client: ::1, server: localhost, request: "GET
/phone/index HTTP/1.1", host: "localhost"
Now the index.php shows a 404 error, what am I doing wrong? Even though the file is there
If you want to avoid a trailing / for all URIs, but still test for index.html and index.php, you should avoid using the index directive and the $uri/ term with the try_files directive.
One approach is to use a named location to test for PHP files. This location will need your fastcgi configuration.
For example:
root /path/to/root;
location / {
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
rewrite ^/(.*)/$ /$1 permanent;
try_files $uri $uri.html $uri/index.html #php;
}
location #php {
try_files $uri.php $uri/index.php =404;
fastcgi_pass ...;
...
}

Nginx serving server and static build

I have one next.js server that is running on port 3000 and I have static build (created with create-react-app), that should be admin panel. So it looks like this
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/city-am-club/admin/build/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /admin/ {
root /usr/share/nginx/myproject/admin/build;
index index.html index.htm;
try_files $uri /index.html;
default_type "text/html";
}
location / {
rewrite /(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
I understand that location should be like this with admni panel, cause location is path after root path.
location / {
root /usr/share/nginx/myproject/admin/build;
index index.html index.htm;
try_files $uri /index.html;
default_type "text/html";
}
Any way, I don't really know how to configure this correct. Right now I cannot get my built files, i tried a lot of different variations of this config. ATM I have a behavior when all my routes location /, even when I try to react /admin it shows me 404 page (custom page of locations / server template).
Try this for your NGINX config.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/city-am-club/admin/build/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
rewrite /(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
location /admin/ {
alias /usr/share/nginx/myproject/admin/build;
index index.html index.htm;
try_files $uri /index.html;
default_type "text/html";
}
}
If the admin path is not /usr/share/nginx/myproject/admin/build then change the alias section.

Ngnix settings can not change the default index.html

I config a static site via nginx, and include my settings under conf.d directory. listen port 80.
But I found when I request the url, nginx always redirect the default page /usr/share/nginx/html/index.html
My configuration does seem work, for all access logs were written to my access log settings, and if I change index.html to some other name(i.html for example) in my directory, and request url mysite.com/i.html, I can access the correct page.
So, it seems that nginx redirect all index.html to the default one.
I tried change default port/server name/root and annotate the default settings, even close selinux, all above doesn't work, it really make me mad.
Can anyone help me?
I'm using nginx version 1.10.2, on CentOS 7.2
and following is my site settings:
# the upstream component nginx needs to connect to
upstream blog {
server 0.0.0.0:80; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# the domain name it will serve for
server_name mysite.com;
charset utf-8;
access_log /var/log/blog/access.log;
error_log /var/log/blog/error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / { try_files $uri #blog; }
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
}
and the /etc/nginx/nginx.conf is
server {
# listen 8000 default_server;
# listen [::]:8000 default_server;
# server_name not;
# root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# location / {
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
This makes no sense:
location / {
try_files $uri #blog;
}
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
The lack of a root directive means that the first try_files will look for files in the default location.
All you need is root and index directives within the server context:
For example:
root /home/work/projects/blog/public;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
See this document for details.

Resources