Nginx reverse proxy server set up

Nginx environment to build

Download Source Package unzip Nginx

Through existing archive

Here you can also download online through yum

wget http://nginx.org/download/nginx-1.13.7.tar.gz

tar -zxf nginx-1.14.2.tar.gz -C [目标路径]

Install nginx

Source files are decompressed after the bag

cd nginx-1.14.2/

Before executing the command, do one thing, resolve dependencies, not otherwise install nginx

A key installation dependencies

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

Start the installation nginx

carried out ./configure


The following represents a success

Compile make

installation make install

Set up a profile

vim conf/nginx.conf

Start command

nginx startup command /usr/local/nginx/sbin/directory

You can be copied to /usr/bin/fast start

./nginx start up

Specifies the startup configuration file

nginx -c /usr/local/nginx/conf/nginx.conf

./nginx -s stop shut down

./nginx -s reload Restart

iptables need to open port 80 otherwise inaccessible,iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Simple to configure port forwarding

Enter profile

vim conf/nginx.conf

Create a new application server to forward port

server {
    # 监听8090端口
    listen  8090;
    location / {
        # 访问8090端口转发到80端口的应用服务器上
        proxy_pass  http://127.0.0.1:80;
    }
}

Restart nginx nginx -s reload

By configuring the load balancing pool to configure port forwarding

Enter profile

vim conf/nginx.conf

# 配置负载均衡池
upstream demo_pool{
    server 127.0.0.1:80;
}
server {
        listen 1234; #拦截端口
        server_name  demo.com; #域名配置
        # access_log logs/bolg.log;
        # error_log logs/bolg.error;
        #将所有请求转发给dome_pool池的应用处理
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://demo_pool; #如果是ssl更改成https
    }
}

access http://192.168.80.11:1234

server_name matches

server {
    listen 80;
    server_name www;
    location / {
        default_type text/html;
        content_by_lua '
            ngx.say("<p>www</p>")
        ';
    }
}
server {
    listen  80;
    server_name www.r0cky.com;
    location / {
        default_type text/html;
        content_by_lua '
            ngx.say("<p>www.r0cky.com</p>")
        ';        
    }
}
server {
    listen 80;
    server_name www.r0cky.*;
    location / {
        default_type text/html;
        content_by_lua '
            ngx.say("<p>www.r0cky.*</p>")
        ';
 
    }
}
server {
    listen 80;
    server_name ~\w+.com;
    location / {
        default_type text/html;
        content_by_lua '
            ngx.say("<p>~\w+.com</p>")
        ';        
    }
}
server {
    listen 80;
    server_name ~.*r0cky.com;
    location / {
        default_type text/html;
        content_by_lua '
            ngx.say("<p>~.*r0cky.com</p>")
        ';
    }
}

Different domain names will pass the request header HOST field, matching the block to a particular server, to forward to a corresponding application server.

hosts

192.168.80.11   www.r0cky.com
192.168.80.11   www.r0cky.org
192.168.80.11   blog.r0cky.com
192.168.80.11   r0cky.com

Access www.r0cky.com


Access www.r0cky.org


Access blog.r0cky.com


Matching order

server_name match with host priority is as follows:

  1. Exact match
  2. Preceding wildcard, such as .test.com
  3. After such www.test. *
  4. Regular matching, as ~ ^ .www.test.com $

If they do not match

  1. After the preferred configuration items have default or listen the default_server
  2. The first block matching to find a server listen port

Guess you like

Origin www.cnblogs.com/r0ckysec/p/11440835.html