Nginx deployment and common configuration methods

overview

Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server,
released under the BSD-like protocol. It is characterized by less memory and strong concurrency.

Nginx official website: http://nginx.org/
Ngin download address: http://nginx.org/download/

Install

Source code installation (high scalability)

Download service pack

wget wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar zxf nginx-1.22.0.tar.gz&&mv nginx-1.22.0 nginx
mkdir /data/src&&mv nginx /data/src

install dependencies

yum -y install pcre pcre-devel zlib-devel gcc gcc-c++ make zlib

Create a startup user

useradd -M -s /sbin/nologin nginx    

Compile and install

cd /data/src/nginx/
./configure --prefix=/data/nginx/ --user=nginx --group=nginx --with-http_sub_status_modele&&make&&make install

Compilation command description:
./configure
Compilation command
–prefix=/data/nginx/
installation path
–user=nginx
specified user
–group=nginx
specified user group
–with-http_sub_status_model
load module (multiple spaces can be loaded, separated by [ ./configure --help] to find the required modules)

Configure environment variables

echo "PATH=$PATH:/data/nginx/sbin/" >>/etc/profile
source /etc/profile

Check Nginx version

nginx -v

View nginx compilation configuration

nginx -V

check configuration file

[root@xl02 data]# nginx -t
nginx: the configuration file /data/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx//conf/nginx.conf test is successful

Start Nginx

Nginx

Nginx status control

#启动服务
nginx
#重载加载
nginx -s reload
#立刻停止服务
nginx -s stop 
#完成当前进程后停止服务
nginx -s quit 
#重新打开日志文件
nginx -s reopen

yum installation (lack of extensibility)

install nginx

yum -y install nginx 

#Configuration file /etc/nginx/
#Log file location /var/log/nginx/
#html file location /usr/share/nginx/html

start service

systemctl start nginx

Container installation (quick and easy)

Download the Nginx mirror

docker pull nginx:1.22.0

docker run deploy

Start the nginx container

docker run -itd --name nginx -p 80:80  nginx:1.22.0

docker-compose deployment

Create a deployment directory

mkdir /data/nginx/{
    
    html,ssl,log,conf.d}

Create master profile

vim /data/nginx/nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Create a server configuration file

vim /data/nginx/conf.d/default.conf
server {
    
    
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    
    
        root   /usr/share/nginx/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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    
    
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    
    
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    
    
    #    deny  all;
    #}
}

Create Nginx Homepage

vim /data/nginx/html/index.html
<html>
welcome nginx!
</html>

Create YML file

vim /data/nginx/docker-compose-nginx.yml
version: '3'

services:

  nginx:
    image: nginx:1.22.0
    container_name: nginx
    restart: always
    ports:
    - 80:80
    - 443:443
    volumes:
    - /data/nginx/nginx.conf:/etc/nginx/nginx.conf
    - /data/nginx/log/:/var/log/nginx
    - /data/nginx/conf.d/:/etc/nginx/conf.d
    - /data/nginx/ssl/:/ssl
    - /data/nginx/html/:/usr/share/nginx/html
    environment:
    - TZ=Asia/Shanghai

Start the container

docker-compose -f /data/nginx/docker-compose-nginx.yml up -d

Visit Nginx

[root@xl02 html]# curl 127.0.0.1
<html>
Welcome to nginx!
</html>
[root@xl02 html]# 

Common configuration methods of Nginx

Nginx log optimization

The nginx log configuration item is added to the http configuration item

http {
    
    
log_format  zdy '[$time_local]-$remote_addr-$scheme/$server_port-"$request_method"-"$uri"-($status)-$request_time s-$body_bytes_sent(bytes) $upstream_addr-($upstream_status)-$upstream_response_time s 第($connection_requests)次请
求'
                    ' body大小:$body_bytes_sent 请求长度:$request_length 发送长度:$bytes_sent';
}

Add log output configuration to the server configuration item

    server {
    
    
        listen       80;
        listen       [::]:80;
        server_name  localhostname;
        root         /usr/share/nginx/html;
        charset utf-8;
        access_log /var/log/nginx/access.log zdy;
}

Restart Nginx (reloading service may not take effect)

systemctl restart nginx 或 nginx -s stop && nginx

nginx foreground start

1. Command start

nginx -g "daemon off;"

2. Edit the nginx configuration file to add configuration items.

daemon off;

stop nginx

nginx -s stop 

start nginx

nginx

nginx forwards TCP port

Note: Use [–with-stream] module, Nginx is not installed by default, it can be added by recompilation.

Edit nginx configuration file

vim /etc/nginx/nginx.conf
#将此配置添加至http{...}配置项下边
include /etc/nginx/tcp.d/*.conf;

Please note that the stream configuration cannot be placed in http, that is, /etc/nginx/conf.d/, because the stream is forwarded through the tcp layer, not http.

Create and configure TCP forwarding configuration files

mkdir -p /data/nginx/tcp.d
vim /data/nginx/tcp.d/mysql.conf
stream {
    
    
upstream mysql{
    
    
server 192.168.2.3:1521;
}
server {
    
    
listen 3306;#将192.168.2.3的1521端口转发到本机的3306端口
proxy_pass mysql;
}
}

Restart the nginx service

systemctl restart nginx 或 nginx -s stop && nginx

Now you can access the 1521/TCP port of 192.168.2.3 through the 3306/TCP of the local IP

Nginx reverse proxy

Add upstream reverse proxy pool - location forward proxy pool

http{
    
    

upstream test{
    
    
    server 10.172.52.53:20001;
    server2 IP:PORT;
}
    server {
    
    
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /test{
    
    
            proxy_pass  http://test;
        }

}

Nginx reverse proxy RabbitMQ management terminal

http{
    
    
upstream mq {
    
    
    server 172.17.34.124:15673;
}
server{
    
    
location /mq {
    
    
            rewrite /mq/(.*)$ /$1 break;
            proxy_pass  http://mq/;
      }
}}

Note: The root of mq access is 127.0.0.1:15672/ or 127.0.0.1:15672/mq This example is: 127.0.0.1:15672/mq
If the root of mq access does not match the location configuration, it will also cause inaccessibility or access white screen

Guess you like

Origin blog.csdn.net/weixin_49566876/article/details/128850159