Using Nginx to deploy multiple Web servers on a single server

Source: Internet
Author: User
Tags config php
This article mainly introduces the use of Nginx in a server to deploy multiple Web server, has a certain reference value, now share to everyone, a friend in need can refer to

Recently tinkering a lot of time, at first think of this is because node directly from the server way a bit of violence, because the use of 80 port is really occupied, it is difficult to use a 80 port to hang multiple WebServer. You can only mount on more than one port, after access to add a port, too ugly, so groping a bit, with Nginx configured a multi-service structure. This allows you to access different applications through different subdomains!

the approximate structure

In essence, the Nginx is responsible for forwarding incoming requests, then the basis for forwarding is to determine the different subdomains

STEP 1 Install Nginx

The first step of course is to install Nginx, the platform is not the same, and can not share. The environment of my own Ubuntu

apt-get isntall Nginx

STEP 2 Set DNS and PORT

Go to your domain manager to assign the subdomain you want, of course a record and all to your VPS public network IP.

Another preparation is to modify all of your nodewebserver ports, can be set according to your preferences, but do not occupy 80 and 443 ports

STEP 3 nginx config. js

Switch to your nginx configuration directory

My directory Location

/etc/nginx/

Here to illustrate that Nginx has an Include mechanism, he will automatically load all the _.conf_ in the CONF.D directory (under the default configuration), so we do not need to modify the nginx.conf file. Next we need to create a series of configuration files in the CONF.D directory, using your corresponding project name, such as project1.conf

If not, manually join

        include/etc/nginx/conf.d/*.conf;        # include/etc/nginx/sites-enabled/*; It's best to annotate him with the default page.

Start Writing project Configurations

CD CONF.D

Touch project1.conf

Vim project1.conf

Let's write a detailed forwarding rule

This is a node Web server//I have changed the port of this service to port 8000//When Nginx captures the domain name as Api.yinode.tech//forwards to the local 8000 port server{    Server_ Name Api.yinode.tech;    Listen;    Location/{        # Proxy_http_version 1.1;        Proxy_set_header Connection "";        Proxy_set_header x-real-ip $remote _addr;        Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;        Proxy_set_header Host $http _host;        Proxy_set_header x-nginx-proxy true;        Proxy_pass Http://127.0.0.1:8000$request_uri;        Proxy_redirect off;    }}

If you need to forward HTTPS, please copy the contents of the above paste to the following, modify the listening port is 443, the forwarding port is the HTTPS port you listen to!

STEP 4 nginx config PHP

PHP forwarding settings will be slightly different, (by default you have installed PHP)

server {  listen;  root/var/www/longqiyoutian/wordpress; Your root directory is  index index.php;  server_name Dragon.yinode.tech;  Error_page 404/404.html;  Location ~ \.php$ {    try_files $uri = 404;    Fastcgi_pass Unix:/var/run/php/php7.0-fpm.sock; Your fpm address is    fastcgi_index index.php;    Include Fastcgi_params;    Fastcgi_param  script_filename  $document _root$fastcgi_script_name;//must be completed, note the Order  }}

Explained, first of all, our root directory will become important, Nginx will send this directory to FPM to run PHP code, so do not write wrong, the second is the back of the forwarding must add fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; This can guide FPM to correct the address stitching

TIPS: Reminders

Nginx and FPM default startup user are Www-data, so your site root directory must modify the owner of the Www-data, and do not put this directory under/root/, there will be permissions issues, it is best to copy to the/var/www directory, and set the owner!

STEP 5 Repeat

Repeat 3 4 steps to create all the project configurations you need

STEP 6 Restart Nginx

Service Nginx Restart

If an error occurs, check the information carefully, go online to find, generally because the configuration file format is wrong.

STEP 7 Run

Open your website, it should be able to run properly! New Server in the future in fact, as long as add a corresponding conf file, can be very convenient horizontal extension, and the entire site will be more beautiful access!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.