The detailed installation steps of Nginx server ---- configure virtual host based on domain name, ip address, port number----- take a look

-----Detailed installation steps of Nginx server

[Introduction to Nginx]

  • Nginx is a high-performance, lightweight web service software, and also an HTTP and reverse proxy server.
  • Nginx features: high stability, low system resource consumption, and high processing capacity for http concurrent connections. A single physical server can support 30,000 to 50,000 concurrent requests.
  • As a web server: Compared with Apache server, Nginx uses fewer resources, supports more concurrent connections, and reflects higher efficiency. This makes Nginx more popular.
  • Support rewrite rewrite rules: According to different domain names and URLs, http requests can be distributed to different back-end server groups.
  • Built-in health check function: If the back-end web server of Nginx Proxy goes down, the front-end access will not be affected.
  • Save bandwidth: support gzip compression, you can add the browser's local cache Header header.
  • High stability: Used for reverse proxy, the probability of downtime is very low.

[Install Nginx server]

1. Build the environment

If the httpd service is installed on the Linux operating system, please close it first, otherwise you will encounter an error that port 80 is occupied during the installation process

1. The configuration and operation of Nginx need the support of pcre (library files provided), zlib (header files provided) software packages, (devel: development package), first install.

[root@localhost~]# yum -y install pcre-devel zlib-devel

2. Create a dedicated user account to more accurately control its access rights, increase flexibility and reduce security risks.

[root@localhost~]# useradd -M -s /sbin/nologin nginx

3. Compile and install Nginx
. An Nginx compressed package is required here. You can download one from the Internet and upload it to the /opt directory. (Mine is nginx-1.15.9.tar.gz version)

[root@localhost opt]# tar zxvf nginx-1.15.9.tar.gz
[root@localhost opt]#cd nginx-1.15.9/
[root@localhost nginx-1.15.9]#
./configure \
--prefix=/usr/local/nginx \            #######安装路径/usr/local/nginx
--user=nginx \              #######运行用户为nginx
--group=nginx \           #######运行用户组为nginx
--with-http_stub_status_module \          ########启用这个模块以支持状态统计,便于查看服务器的连接信息
(这边建议 先将 gcc gcc-c++ make 编译语言一起安装下,因为我的是装过的了,怕你们会报错)
[root@localhost nginx-1.15.9]# make -j3                     ######make编译,核心数为3(加载速度快些)
[root@localhost nginx-1.15.9]# make install                     ######make 安装

4. Create a soft connection file for the main program Nginx, so that the administrator can directly execute the "nginx" command to call the main program of Nginx.

[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]#  ls -l /usr/local/sbin/nginx
(####问题报错:可能用户账号没创建 或者 make没编译好,需要重新make编译)

2. Nginx operation and control

5. The Nginx main program also provides the "-t" option to check the configuration file in order to find out improper or wrong configuration, (/usr/local/nginx/conf/nginx.conf)
if you want to check other locations Configuration file, you can use the "-c" option to specify the path.

[root@localhost nginx-1.15.9]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.15.9]# nginx            ######直接运行 nginx 及可启动 nginx 服务器
(注意这里:若服务器里有 httpd 等其他 web 服务软件,可以修改端口、停用或卸载服务,要不然会冲突。)

6. By checking the monitoring status of the Nginx program, or accessing the web service in the browser (the default page will display "Welcone to nginx!"), you can confirm whether the Nginx service is running normally.

[root@localhost nginx-1.15.9]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      76079/nginx: master
[root@localhost nginx-1.15.9]# yum -y install elinks               ######安装elinks浏览器
[root@localhost nginx-1.15.9]#elinks http://localhost                 ######使用elinks浏览器
1)://会显示: “Welcone to nginx!” 页面,表明 nginx 服务在正常运行
2)://   在你可以在真机上访问域名时,再在 linux 操作系统上输入 “elinks http://localhost ”时,将会显示你的域名

7. Use the killall command to reload the configuration and stop the service (specify the signal type through the "-s" option), signal (HUP reload configuration; QUIT exits the process; KILL kills the process)

[root@localhost nginx-1.15.9]# killall -s HUP nginx                       #####重载配置
[root@localhost nginx-1.15.9]# killall -s QUIT nginx                       #####退出进程

8. Use systemctl tool to manage based on Nginx service script

[root@localhost nginx-1.15.9]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
 
[root@localhost nginx-1.15.9]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.15.9]# systemctl enable nginx.service

Installed

[Configure virtual host based on domain name]

1. Modify the C:\Windows\System32\drivers\etc\hosts file of the windos client, and add the two domain names www.51xit.top and www.52xit.top, both of which point to the same server IР address for implementation Different domain names visit different virtual hosts.
For example:
20.0.0.33 www.53com
20.0.0.33 www.553xit.com

2. Prepare the catalog and test homepage of each website

[root@localhost~]# mkdir -p /var/www/html/53xit/                  ####创建www.53xit.com的根目录
[root@localhost ~]# mkdir -p /var/www/html/553xit/                      ####创建www.553xit.com的根目录
[root@localhost~]# echo "www.53xit.top" >> /var/www/html/53xit/index.html
[root@localhost~]#echo "www.553xit.top" >> /var/www/html/553xit/index.html

3. Edit the configuration file nginx.conf

    [root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
    #user  nobody;                                                  #####运行用户
    worker_processes  1;                                        #####工作进程数量
    #error_log  logs/error.log;                               #####错误日志文件的位置
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    #pid        logs/nginx.pid;                                   #####PID文件的位置
     
    events {
        use epoll;                                                       #####使用 epoll 模型
        worker_connections  1024;                         #####每个进程处理1024个连接
    }
     
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  logs/access.log  main;                                  #####访问日志位置
        sendfile        on;                                    #####支持文件发送(下载)
        #tcp_nopush     on;
        #keepalive_timeout  0;
        keepalive_timeout  65;                                     #####连接保持超时
        #gzip  on;
         
#    server {                                                #####web 服务的监听配置
#        listen       80;                                   #####监听端口及地址
#        server_name  localhost;                            #####网站名称(FQDN)
#        charset utf-8;                                   #####网页的默认字符集      
#        #access_log  logs/host.access.log  main;
#        location / {                                   #####根目录配置
#            root   html;                                #####网站根目录的位置
#           index  index.html index.htm;                   #####默认首页(索引页)
#       }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;             #####内部错误的反馈界面
#       location = /50x.html {                                          #####错误页面配置
#            root   html;
#        }
 
     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     #location ~ \.php$ {
     #    proxy_pass   http://127.0.0.1;
     。。。。。。。                #####省略内容,以下全 # 号注释
     。。。。
     #location ~ /\.ht {
     #    deny  all;
     #}
#    }
  
server {                       #####直接插入添加(第一个域名)
      listen 80;
      server_name www.53xit.com;
      charset utf-8;
      access_log logs/www.53xit.com.access.log;
      location /{
             root /var/www/html/53xit;
             index index.html index.htm;
       }
      error_page 500 502 503 504 /50x.html;
      location = 50x.html{
                  root html;
                   }
}
 
server {                                     #####直接插入添加(第二个域名)
     listen 80;
     server_name www.553xit.com;
     charset utf-8;
     access_log logs/www.553xit.com.access.log;
     location /{
             root /var/www/html/553xit;
             index index.html index.htm;
     }
     error_page 500 502 503 504 /50x.html;
     location = 50x.html{
                   root html;
                   }
}
 
 # another virtual host using mix of IP-, name-, and port-based configuration
          ........(省略内容)

4. Start Nginx

[root@localhost nginx-1.15.9]# systemctl restart nginx

5. Enter the domain name in the real machine browser to test

[Configure virtual host based on ip address]

1. Add a network card to the virtual machine, set ip, and test ping

Add custom: VM1 network card;
view PPID number: nmcli connection
copy the PPID number of ens36
cd /etc/sysconf/network-scripts/
cp ifcfg-ens33 ifcfg-ens36
vi ifcfg-ens36
… (change the content inside, pay attention to change the PPID number )

Restart the network card: systemctl restart network

2. Modify the configuration in the sub-definition tag in the nginx.conf configuration file

vi /usr/local/nginx/conf/nginx.conf
        。。。。。。(省略内容,只需要改下面几个参数)
erver {
        listen 192.168.100.33:80;                                 #####修改成ip
        server_name 192.168.100.33:80;                      #####修改成ip
        charset utf-8;
        access_log logs/www.53xit.com.access.log;
        location / {
                root /var/www/html/53xit;
                index index.html index.htm;
                }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
                root html;
                }
}
 
server {
        listen 20.0.0.33:80;                                   #####修改成ip
        server_name 20.0.0.33:80;                        #####修改成ip
        charset utf-8;
        access_log logs/www.553xit.com.access.log;
        location /{
                root /var/www/html/553xit;
                index index.html index.htm;
                }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
                root html;
                }
}

Restart the nginx service: systemctl restart nginx

3. Enter the IP address in the real machine browser to test

[Configure virtual host based on port number]

1. The same ip address but different port numbers

vi /usr/local/nginx/conf/nginx.conf
              。。。。。(省略以下内容,更改如下几个参数)

server {
        listen 20.0.0.33:6666;
        server_name 20.0.0.33:6666;
        charset utf-8;
        access_log logs/www.53xit.com.access.log;
        location / {
                root /var/www/html/53xit;
                index index.html index.htm;
                 }
                   error_page 500 502 503 504 /50x.html;
      location = 50x.html{
               root html;
               }
}
 
server {
      listen 20.0.0.33:8888;
      server_name 20.0.0.33:8888;
      charset utf-8;
      access_log logs/www.553xit.com.access.log;
      location /{
            root /var/www/html/553xit;
            index index.html index.htm;
             }
      error_page 500 502 503 504 /50x.html;
      location = 50x.html{
               root html;
               }
}

Restart the nginx service: systemctl restart nginx

2. Enter the IP address + port number in the browser of the real machine

Guess you like

Origin blog.csdn.net/XCsuperman/article/details/108613705