nginx (solve the problem of multiple domain name resolution)

1. Use a domain name to access local projects

We will use the following domain name:
main domain name: www.leyou.com
management system domain name: manage.leyou.com
gateway domain name: api.leyou.com

In the end, these domain names point to a port on our machine.

1.1 Domain name resolution

A domain name must be resolved into one or more IPs. There are generally two steps:

  • Local domain name resolution The
    browser will first search for the IP address of the domain name mapping in the hosts file of this machine. If it finds it, it returns the ip address. If it is not found, it will resolve the domain name server.
    The hosts file under Linux: / etc / hosts
  • Domain name server resolution
    fails when local resolution fails. Domain name server is a computer on the network, which records all registered domain names and ip mapping relationships. Generally, as long as the domain name is correct and the registration is passed, it must be able to turn up.
    Use the SwithHostst tool In Insert picture description here
    this way, only use www.leyou.com:9001 to access. Therefore, we have to solve the port problem so that his direct domain name can be accessed.

1.2nginx solves the port problem

1.2.1 What is nginx

High-performance web and reverse proxy server.

1.2.2 web server

There are 2 types of web services:

  • web application server
    tomcat
    resin
    jetty

  • Web server
    apache server
    nginx
    IIS
    distinction: The web server cannot parse jsp and other pages, and can only handle static resources such as js, css, html, etc.
    Concurrency: The concurrency capability of the web server is much higher than that of the web application server.
    nginx + tomcat

1.2.3 nginx as a reverse proxy

What is a reverse proxy?

  • Proxy: Through the configuration of the client, let a server proxy the client. All requests from the client are handed over to the proxy server.
  • Reverse proxy: A server is used to proxy the real server. When the user accesses, it is no longer a real server, but a proxy server.
    nginx can be used as a reverse proxy server:
  • We need to configure the reverse proxy rules in nginx in advance, different requests are handed over to different real servers for processing
  • When the request reaches nginx, nginx will forward the request according to the defined rules, so as to realize the routing function.
    Installed on the host:
    Insert picture description here
    Installed on the virtual machine:

Insert picture description here

1.2.4 linux install ftp service

Installation package

yum -y install vsftpd
Insert picture description here

Modify the configuration file:

vi /etc/vsftpd/vsftpd.conf
ensure the following 3 items are YES

anonymous_enable=YES

anon_upload_enable=YES

anon_mkdir_write_enable=YES

Insert picture description here
By default, vsftp does not allow root users to log in.
Comment out root
Insert picture description here
, change the enforcing mode in / etc / selinux / config to disabled, and restart

Set vsftpd to boot

systemctl start vsftpd.service
systemctl status vsftpd.service
Insert picture description here

Upload and unzip nginx

Insert picture description here
Insert picture description here

Configuration

Enter the nginx directory -prefix
: installation directory
-sbin-path: put the script file in this directory
. / Configure --prefic = / opt / nginx --sbin-path = / usr / bin / nginx
Insert picture description here
here refers to the installation directory in / under opt / nginx

If the configuration fails, install the required packages

yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++
yum -y install pcre-devel
yum -y install openssl openssl-devel

If the configuration is successful, compile and install

make && make install
Insert picture description here

start up

nginx: start
nginx -s stop: stop
nginx -s reload: reload
Insert picture description here
master process nginx: main process, play a monitoring and management role
worker pricess: Really process user requests.

test

Insert picture description here

1.2.5 Configure Multi-Domain Access

Add in the nginx.conf file in / opt / nginx / config in the installation directory:

 server {
        listen       80;
        server_name  manage.leyou.com;
		proxy_set_header X-Forwarded-Host $host;
		proxy_set_header X-Forwarded-Server $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
		#代理转发到该地址
            proxy_pass http://192.168.188.108:9001 ;
            proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }
	 server {
        listen       80;
        server_name  api.leyou.com;
		proxy_set_header X-Forwarded-Host $host;
		proxy_set_header X-Forwarded-Server $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
            proxy_pass http://192.168.188.108:10010;
            proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }

Restart
nginx -s reload

Configure the host file

192.168.188.114 www.leyou.com
192.168.188.114 manage.leyou.com
192.168.188.114 api.leyou.com

Modify project configuration

Modify localhost to 0.0.0.0, that is, any IP can be accessed.
Insert picture description here

Domain access test

Note: linux should turn off the firewall.
Insert picture description here

Published 28 original articles · Likes0 · Visits 900

Guess you like

Origin blog.csdn.net/weixin_43876557/article/details/102823629
Recommended