Centos7 compiles and installs nginx and sets the reverse agent

Keywords: Linux Nginx SELinux yum glibc

I. compile and deploy Nginx 1.12

Installation configuration:

[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -s /sbin/nologin -g nginx -M nginx
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
#Turn off selinux
#Server file descriptors, etc

[root@localhost ~]# yum install gcc gcc-c++ glibc automake pcre zlip zlib-devel openssl-devel pcre-devel wget lrzsz
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@localhost src]# tar -zxvf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --with-pcre
[root@localhost nginx-1.12.0]# make
[root@localhost nginx-1.12.0]# make install

Remarks:

--prefix: Nginx installation directory
--User: Nginx user
--Group: the group of Nginx user
--With HTTP? SSL? Module: provide https support
--With HTTP? Flv? Module: used to build flv video server
--With HTTP? Stub? Status? Module: open the Stub Status module, which will generate a server status and information page
--With HTTP > Gzip > static > module: opens the Gzip static module, which is used to send pre compressed files
--With PCRE: perl execution file path


Configure services:

[root@localhost nginx-1.12.0]# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking  
PIDFile=/usr/local/nginx/logs/nginx.pid  
ExecStartPre=/usr/bin/rm -f /run/nginx.pid  
ExecStartPre=/usr/local/nginx/sbin/nginx -t  
ExecStart=/usr/local/nginx/sbin/nginx  
ExecReload=/bin/kill -s HUP $MAINPID  
KillMode=process  
KillSignal=SIGQUIT  
TimeoutStopSec=5  
PrivateTmp=true
[Install]
WantedBy=multi-user.target


Verification:

[root@localhost nginx-1.12.0]# chmod a+x /usr/lib/systemd/system/nginx.service
[root@localhost nginx-1.12.0]# /usr/local/nginx/sbin/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.12.0]# systemctl start nginx
[root@localhost nginx-1.12.0]# ps -ef | grep nginx
root      24412      1  0 16:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     24413  24412  0 16:31 ?        00:00:00 nginx: worker process
root      24415  10541  0 16:31 pts/0    00:00:00 grep --color=auto nginx


Visit:

http://192.168.146.136




II. nginx configuration reverse agent

Set nginx configuration file and include proxy.conf file:

[root@localhost conf]# cat nginx.conf
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            proxy_pass    http://127.0.0.1:7777;
            include    proxy.conf;
        index  index.php index.html index.htm;
        }
        
        #The following three lines do not need to be added
        location ~ .*\.(js|css|jpg|png)$ {
             proxy_pass http://127.0.0.1:7777;
        }
        ****************

Note: if only one line of proxy pass information is added, the page may be accessible, but css and other style files cannot be loaded, and only text information can be displayed.


proxy.conf configuration file:

[root@localhost conf]# cat proxy.conf
# proxy.conf
client_body_buffer_size     256k;
client_body_temp_path       client_body 1 2;
client_max_body_size        50m;
proxy_buffers               4 32k;
proxy_buffer_size           4k;
proxy_busy_buffers_size     64k;
proxy_connect_timeout       30;
proxy_read_timeout          60;
proxy_redirect              off;
proxy_send_timeout          30;
proxy_set_header            Host $host;
proxy_set_header            X-Server-Addr $server_addr;
proxy_set_header            X-Remote-Addr $remote_addr;
proxy_set_header            X-Remote-Port $remote_port;
proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header            X-Forwarded-Port $server_port;
proxy_set_header            X-Forwarded-Proto $scheme;
proxy_set_header            Server-Type nginx;
proxy_temp_file_write_size  64k;
proxy_temp_path             proxy_temp 1 2;
proxy_max_temp_file_size    128m;
proxy_next_upstream         error timeout invalid_header http_500 http_503 http_404;


Restart nginx service:

[root@localhost conf]# systemctl stop nginx
[root@localhost conf]# systemctl start nginx


Posted by MCP on Mon, 02 Dec 2019 10:13:31 -0800