Skip to content

Instantly share code, notes, and snippets.

@hritik4651
Forked from leommoore/nginx_basics.md
Created March 2, 2024 01:13
Show Gist options
  • Select an option

  • Save hritik4651/b29a50ff3ccb1e15e7b427ccc80ba6ce to your computer and use it in GitHub Desktop.

Select an option

Save hritik4651/b29a50ff3ccb1e15e7b427ccc80ba6ce to your computer and use it in GitHub Desktop.
Nginx Basics

#Nginx Basics for Ubuntu

To install

sudo apt-get install nginx

Make sure that Apache (if present) is not running. You can only have one listening on port 80.

sudo apachectl stop

To start nginx (Linux)

sudo /etc/init.d/nginx start

To stop nginx (Linux)

sudo /etc/init.d/nginx stop

To reload nginx config (Linux)

sudo /etc/init.d/nginx reload

Location of sites (Linux)

/usr/share/nginx/www/

To check to see if it is running

ps -ef | grep nginx

To edit the nginx Config File

sudo vim /etc/nginx/sites-available/default

##Sample file with virtual sites

http://www.test.com
http://www.example.com
http://www.nodeapp.com
http://localhost

The hosts file can be edited at:

sudo vim /etc/hosts

server {
        listen 80;
        listen [::]:80;
        root /home/www/test.com;
        server_name www.test.com;
        
        location / {
           index index.htm index.html;          
        }
}

server {
        listen 80;
        listen [::]:80;
        root /home/www/example.com;
        server_name www.example.com;
        
        location / {
           index index.htm index.html;          
        }
}

#Passthrough to node server application
server {
        listen 80;
        listen [::]:80;
        root /home/www/nodeapp.com;
        server_name www.nodeapp.com;
        
        location / { 
            #proxy_pass http://127.0.0.1:3001/api; 
            proxy_pass http://127.0.0.1:8443;         
        }

server {
	listen 80 default_server;
        listen [::]:80 ipv6only=on default_server;

	root /usr/share/nginx/html;
	index index.html index.htm;

	# Make site accessible by specifying the wildcard servername
	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ /index.html;
	}       
}

##Permissions Nginx normally runs under the www-data group. This group needs to have read access to the folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment