Search

Nginx's Tags

Install Nginx HTTP server on Ubuntu
Nginx is an open source HTTP web server that is often used as reverse proxy or HTTP cache. Nginx is popular for high-performance, speed, stability and low resource consumption. In this article, we will walk through installing and setting up NginX server on Ubuntu. Follow these steps to install and setup NginX on your ubuntu. Requirement You need to be logged in non-root user with sudo permission.  Installing Nginx Nginx is available Ubuntu's default repository, so you can directly install with below command. sudo apt-get update sudo apt-get install nginx After installing it, open your browser and run your server IP address. If you see this page, then you have successfully installed Nginx on your Ubuntu. This default page placed at /var/www/html/ location. Configure Nginx Check ufw application list with below command. sudo ufw app list Add Nginx to use port 80. sudo ufw allow 'Nginx HTTP' If you want to check Nginx server status, then you can run following command. sudo systemctl status nginx Add websites to Nginx When using multiple websites, you need to add your websites to Nginx configuration. For that first create a new Nginx configration file. sudo nano /etc/nginx/sites-available/website And put the site details in it. server {     listen 80;     listen [::]:80;     root /var/www/html/website;     index index.html index.htm index.nginx-debian.html;     server_name website www.website.com;     location / {             try_files $uri $uri/ =404;     } } Now create a website folder /var/www/html/website. And also give 755 permission. sudo mkdir /var/www/html/website sudo chmod -R 7555 /var/www/html/website Now create simple page for website homepage. sudo nano /var/www/html/website/index.html And put any HTML code to check. <!DOCTYPE html> <html> <head>     <title>Hello World!</title> </head> <body>     <h1>Hello World!</h1>     <p>Welcome to Website.com</p> </body> </html> Save this file. And now restart the Nginx server. sudo systemctl restart nginx Now open your and open your website. you should get your page. Conclusion You have completed to install Nginx server and host websites on your Server. There are many features you can add or change in Nginx. You can check the Nginx official documentation.
How to remove NGINX from Ubuntu
After removing nginx server, sometimes you might wonder that it still runs on /var/www/html folder when you run http://localhost in your browser. In this article, we will discuss how to completely remove Nginx server from Ubuntu. In your Terminal, first run the below command to install nginx config files and remove from Ubuntu. sudo apt-get purge nginx nginx-common After running the above command, use below command in order to remove all dependencies used by nginx which are no longer required. sudo apt-get autoremove Further, you can also remove nginx config files which are not required. rm -rf /etc/nginx This way, you can completely remove nginx web server from Ubuntu system.
How to Enable CORS in NGINX
CORS or Cross-Origin Resource Sharing is a protocol that will provide access resources to different domain. This is most commonly used for Javascript requests. The main use for CORS is to use CDN files like css, javascript or images load from different server. In this article, we will discuss how you can allow CORS policy for Nginx server. This can be done through adding header Access-Control-Allow-Origin *. add_header option in Nginx server will add the new header to request. So if you want all request set the header, you need to add the header in default Nginx config file. The default Nginx confinguration file located at /etc/nginx/nginx.conf. So lets edit file in nano editor. sudo nano /etc/nginx/nginx.conf And add the below option in the server key. server {     add_header Access-Control-Allow-Origin *; } Save and exit the file and restart the Nginx server. sudo service nginx restart That's it. Now every response will add the header Access-Control-Allow-Origin: *. To test it, run any site URL in Postman app and see the response header. I hope you liked this article and help on your work.