Nginx keeps showing Welcome to Nginx - nginx

I am using Cent OS 6.1.
I installed Nginx by ./configure method from source. I started the nginx server by sudo nginx and it can serve the Welcome to Nginx page.
However, when I edit the /usr/local/nginx/conf/nginx.conf file, I found that changing the ...location / {... }... block has no effects.
For example, changing
location / {
root html;
index index.htm index.html;
}
to
location / {
root xyz123; #which does not exist
index index.htm index.html;
}
should give 404. But it keeps showing the welcome page.
Even I remove the whole location block, it still shows the welcome page. But if I change the /usr/local/nginx/html to /usr/local/nginx/htmlxyz it shows 404. Is there another conf file running that overridden the nginx.conf?
p.s. I did sudo nginx -s stop then sudo nginx or sudo nginx -s reopen but didnt help :(

Why you install over EPEL. I've installed 10 nginx server just like that and it is working with Node.js.
Maybe you can get some error ? Show full ./configure.
Example Nginx / Php / Mysql for Centos :
https://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-centos-6.4
Default centos nginx html path :
/usr/share/nginx/html/

Related

Basic clean wordpress install on Nginx returns 502 error

I’m learning about running a server on a raspberry pi and just want to run a simple default wordpress site served with Nginx. For some reason loading the site locally in a browser returns a 502 error despite my other basic non-wordpress sites loading correctly. A clean download of the default wordpress installation files are inside /var/www/wp.example.co.uk
I’ve made a wp.example.co.uk.conf file inside /etc/nginx/sites-available - also symlinked to /etc/nginx/sites-enabled - with the code:
upstream wp-php-handler {
server unix:/var/run/php/php7.3-fpm.sock;
}
server {
listen 5432;
server_name _;
root /var/www/wp.example.co.uk;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass wp-php-handler;
}
}
Whenever I view it in a browser (http://mylocalip:4323) it returns a 502 error. Why does this happen?
Note: I’m following a YouTube tutorial (where the relevant part is ~6:43 of https://www.youtube.com/watch?v=8psimaAr1U8) that shows the same code working, which leads me to believe that my code should work as-is.
Thanks
It looks like the tutorial might be outdated after only 6 months. It tells you to install php-fpm, and then it just assumes that version 7.3 is going to be installed. If you run the command apt show php-fpm | grep "Depends:" it'll tell you which version is actually being installed. Now while you could just run apt install php7.3-fpm to follow along with the tutorial, I've included some instructions below on how to use a more recent version of PHP.
Install the version you want e.g. apt install php8.1-fpm or just apt install php-fpm for the current default version.
Run ls -d /var/run/php/* | grep sock --color=never to view all of the versions of PHP-fpm that are available on your system for you to use. The version that you just installed should be listed here.
In the line of your config file that says server unix:/var/run/php/php7.3-fpm.sock;, replace the file referenced with one of the files listed in step 2.
Don't forget to reload Nginx when done. On Ubuntu and Debian systems this is done with the command sudo systemctl reload nginx.

nginx serve a specific directory for a specific location

I need for development purpose (not for production of course), to serve my logs that are in my folder /app/logs/.
So I've setup a configuration like this :
server {
listen 80;
server_name server-name.com;
error_log /app/logs/error.log warn;
access_log /app/logs/access.log compression;
root /var/www;
location /info/logs/ {
alias /app/logs/;
autoindex on;
}
location / {
// others route working
}
}
But everytimes i try to access to something like /app/logs/django.log using http://server-name.com/info/logs/django.log, I get a 404 and not the file I asked for.
I've tried many things like chmod -R 755 the entire folder or setting the folder to the user nginx use (in my case for now root, I know it's bad), tried root or try_files but I just can't access it...
I've seen many topics here and there but can't find a clue...
Can you help me with this please ?
PS : I need the root /var/www at the beginning for others locations.
PS2 : I'm using a Docker based on Debian 9.
If you are using the standard NGINX Docker container, then the default configuration is to send the Access and Error logs to the Docker log collector. Not the standard log output for NGINX.
The result is that your log files are most likely going to:
file/var/lib/docker/containers/<container id>/<container id>-json.logon the Docker Host. Where <container id> is the long-form version of the Container Id specified when the container was setup in the first place.
To get to the default log files you can do: docker logs <container name>
To find the <container id> you can do: docker inspect --format '{{ .Id }}' <container name>
If you want to customize where the log files go, then you need to create a helper Docker container that the log files can be written too.
Ok, I've found my problem, I had a route like this :
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
And I didn't know that despite the fact it was after the first location, it would be choosen over the other location...
Thanks for your help anyway !

Simple nginx config not working on CentOS

I have installed Nginx 1.12.2 on CentOS 7. I have an extremely simple nginx config and it is not working at all. I have setup several nginx instances on Ubuntu in the past without any issue I wonder if there is something to do with CentOS.
I have double-checked that the "root" directory exists and the files also exist with proper permissions. But I am getting 404 error. Also for debugging purpose, I tried to put "return 200 $uri" in the location block and it seems to be returning me the proper URI but try_files doesn't work
/var/www/mydomain/public/test.html exists with proper permissions
For debugging when I put "return 200 $uri" it shows up when I hit the domain
Hitting mydomain.com/test.html gives 404
server {
listen 80;
root /var/www/mydomain/public;
index index.html index.htm;
server_name mydomain.com;
location / {
# return 200 "$uri";
try_files $uri $uri/;
}
}
Few things:
Check your NGINX error log at /var/log/nginx/error.log, you will likely see what file is being accessed and make conclusions from that
Be aware of the presence of /etc/nginx/conf.d/default.conf, which is shipped with the package. It has default server, which is what NGINX will use when no domain has matched, however it's a sample file rather than a real config. I tend to just echo > /etc/nginx/conf.d/default.conf, to "remove it" in a safe way. (If you just remove the file, then package update will restore it, but if you nullify it like I do, then package upgrades won't touch it).

Nginx can't serve static content and return 403(Forbidden) error page

I am new to nginx and trying to serve static contents with nginx and getting 403 error.I have server config like this:
server {
listen 8000;
server_name localhost;
root /Users/ismayilmalik/Documents/github/nginx-express;
location / {
index index.html;
}
I have executed commands below:
chmod -R 755 /nginx-express
chmod -R 644 /nginx-express/*.*
And the folder has drwxr-xr-x rigt.What's wrong here?
Please go to your nginx error logs to get details.
Run this command to show last errors:
tail -20 /var/log/nginx/error.log
It's good to go through error logs located /var/log/nginx/***.error. I had problems similar to this once. The solution was the user nginx was running as.
If nginx is running as www then www will not have access to ismayilmalik folders unless you also grant access to /Users/ismayilmalik home folder, but that is not secure. The best solution would be to allow nginx to run as ismayilmalik if you want to access your home folder through nginx.
I solved it finally.Actually nginx had all permission to serve static content from:
/Users/ismayilmalik/Documents/github/nginx-express;
The reason was when started nginx could not create error.log file in it'sroot directory. After manually creating the file it worked fine. I am using macOs and to find logs folder executed the command below to find all enironment variables for nginx:
nginx -V
BTW before this I had changed nginx user to from nobody to admin in main config file like below.
user [username] [usergroup]
By default nginx master process runs under root and child process under nobody.

How can I deploy my Angular 2 + Typescript + Webpack app

I am actually learning Angular 2 with Typescript and developed a little app by based on the angular-seed project (angular-seed). I have built the app for production purposes and got dist folder ready to be deployed containing my bundle files like this:
dist/
main.bundle.js
main.map
polyfills.bundle.js
polyfills.map
vendor.bundle.js
vendor.map
However, as a fresher, I have no idea how to deploy it now on my EC2 server. I read that I have to config Nginx server to serve my static file but do I have to config it particularly to work with my bundle files?
Excuse my mistakes if any. Thanks a lot in advance!
You are on the right track.....
Just install the nginx on your EC2. In my case I had a linux Ubuntu 14.04 installed on "Digital Ocean".
First I updated the apt-get package lists:
sudo apt-get update
Then install Nginx using apt-get:
sudo apt-get install nginx
Then open the default server block configuration file for editing:
sudo vi /etc/nginx/sites-available/default
Delete everything in this configuration file and paste the following content:
server {
listen 80 default_server;
root /path/dist-nginx;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
}
To make the changes active, restart the webserver nginx:
sudo service nginx restart
Then copy index.html and the bundle files to /path/dist-nginx on your server and you are up and running.
If anyone still struggling with production setup of angular 2/4/5 app + Nginx, then here is the complete solution:
Suppose you want to deploy your angular app at HOST: http://example.com and PORT: 8080
Note - HOST and PORT might be different in your case.
Make sure you have <base href="/"> in you index.html head tag.
Firstly, go to your angular repo (i.e. /home/user/helloWorld) path at your machine.
Then build /dist for your production server using the following command:
ng build --prod --base-href http://example.com:8080
Now copy /dist (i.e. /home/user/helloWorld/dist) folder from your machine's angular repo to the remote machine where you want to host your production server.
Now login to your remote server and add following nginx server configuration.
server {
listen 8080;
server_name http://example.com;
root /path/to/your/dist/location;
# eg. root /home/admin/helloWorld/dist
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
# This will allow you to refresh page in your angular app. Which will not give error 404.
}
}
Now restart nginx.
That's it !! Now you can access your angular app at http://example.com:8080
Hope it will be helpful.
A quicker way to deploy is as below:
1. Install nginx as mentioned by Herman.
2. Copy your dist/* files to /var/www/html/ without disturbing /etc/nginx/sites-available/default.
sudo cp /your/path/to/dist/* /var/www/html/
3. Restart nginx:
sudo systemctl restart nginx
I'm using the official angular CLI instead to deploy to production and is very easy to do. You can deploy to pre-production ie, or production this way:
ng build --env=pre --output-path=build/pre/
ng build --env=prod --output-path=build/prod/

Resources