Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Centos7.2-based nginx deployment

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Centos7.2-based nginx deployment

Deployment background: use Nginx as the load balancer for Tomcat.

Deployment steps:

Install dependent packages such as zlib-devel, pcre-devel, etc.

[root@nginx ~] # yum install-y gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

Note: combine proxy and upstream modules to achieve back-end web load balancing

Combine the default ngx_http_proxy_module module and ngx_http_upstream_module module of nginx to realize the health check of the back-end server.

Proxy: implement reverse proxy

Upstream: achieving load balancing

Nginx uses the openssl-devel module when using HTTPS services. If you do not install openssl-related packages, the process of installing Nginx will report an error.

Create a nginx user

[root@nginx] # useradd-s / sbin/nologin www

[root@nginx ~] # grep www / etc/passwd # # check whether the nginx user www is established

Www:x:1000:1000::/home/www:/sbin/nologin

Compile and install nginx

[root@nginx src] # tar-zxvf nginx-1.13.0.tar.gz

[root@nginx src] # cd nginx-1.13.0

[root@nginx nginx-1.13.0] # / configure-- prefix=/usr/local/nginx1.10-- user=www-- group=www-- with-http_stub_status_module-- with-http_realip_module-- with-http_ssl_module-- with-http_gzip_static_module-- with-pcre-- with-http_flv_module & & make & & make install

Where:-- prefix=/usr/local/nginx1.10 indicates the installation path of the nginx package

Create nginx soft connection to facilitate the execution of nginx programs

[root@nginx nginx-1.13.0] # ln-s / usr/local/nginx1.10/sbin/nginx / usr/local/sbin/

Nginx syntax check

[root@nginx nginx-1.13.0] # nginx- t

Scripting nginx services

[root@nginx ~] # vim / etc/init.d/nginx

#! / bin/sh

#

# nginx-this script starts and stops the nginx daemon

#

# chkconfig:-85 15

# description: Nginx is an HTTP (S) server, HTTP (S) reverse\

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: / usr/local/nginx1.10/conf/nginx.conf

# pidfile: / usr/local/nginx1.10/logs/nginx.pid

Nginxd=/usr/local/nginx1.10/sbin/nginx

Nginx_config=/usr/local/nginx1.10/conf/nginx.conf

Nginx_pid=/usr/local/nginx1.10/logs/nginx.pid

RETVAL=0

Prog= "nginx"

# Source function library.

. / etc/rc.d/init.d/functions

# Source networking configuration.

. / etc/sysconfig/network

# Check that networking is up.

["$NETWORKING" = "no"] & & exit 0

Nginx= "/ usr/local/sbin/nginx"

Prog=$ (basename $nginx)

NGINX_CONF_FILE= "/ usr/local/nginx/conf/nginx.conf"

Lockfile=/var/lock/subsys/nginx

Start () {

[- x $nginx] | | exit 5

[- f $NGINX_CONF_FILE] | | exit 6

Echo-n $"Starting $prog:"

Daemon $nginx-c $NGINX_CONF_FILE

Retval=$?

Echo

[$retval-eq 0] & & touch $lockfile

Return $retval

}

Stop () {

Echo-n $"Stopping $prog:"

Killproc $prog-QUIT

Retval=$?

Echo

[$retval-eq 0] & & rm-f $lockfile

Return $retval

}

Restart () {

Configtest | | return $?

Stop

Start

}

Reload () {

Configtest | | return $?

Echo-n $"Reloading $prog:"

Killproc $nginx-HUP

RETVAL=$?

Echo

}

Force_reload () {

Restart

}

Configtest () {

$nginx-t-c $NGINX_CONF_FILE

}

Rh_status () {

Status $prog

}

Rh_status_q () {

Rh_status > / dev/null 2 > & 1

}

Case "$1" in

Start)

Rh_status_q & & exit 0

, 1

Stop)

Rh_status_q | | exit 0

, 1

Restart | configtest)

, 1

Reload)

Rh_status_q | | exit 7

, 1

Force-reload)

Force_reload

Status)

Rh_status

Condrestart | try-restart)

Rh_status_q | | exit 0

*)

Echo $"Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest}"

Exit 2

Esac

Add a self-boot service

[root@nginx ~] # chmod + x / etc/init.d/nginx

[root@nginx] # chkconfig-- add nginx

[root@nginx ~] # chkconfig nginx on

[root@nginx ~] # chkconfig-- list | grep nginx

Nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Start the nginx service

[root@nginx ~] # / usr/local/sbin/nginx start

Nginx: invalid option: "start"

[root@nginx ~] # / etc/init.d/nginx start

Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl-xe" for details.

[FAILED]

As we can see above, nginx failed to start! Here is the solution:

[root@nginx ~] # / usr/local/sbin/nginx

[root@nginx ~] # / etc/init.d/nginx start

Starting nginx (via systemctl): [OK]

Configure nginx reverse proxy: function is (reverse proxy + load balancing + health detection)

Modify the nginx main configuration file:

[root@nginx ~] # vim / usr/local/nginx1.10/conf/nginx.conf

User www www

Worker_processes 2

Worker_cpu_affinity 0101 1010

Error_log logs/error.log

# error_log logs/error.log notice

# error_log logs/error.log info

Worker_rlimit_nofile 10240

Pid logs/nginx.pid

Events {

Use epoll

Worker_connections 4096

}

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

Server_tokens off

Sendfile on

Tcp_nopush on

# keepalive_timeout 0

Keepalive_timeout 65

# Compression Settings

Gzip on

Gzip_comp_level 6

Gzip_http_version 1.1

Gzip_proxied any

Gzip_min_length 1k

Gzip_buffers 16 8k

Gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascriptapplication/xml

Gzip_vary on

# end gzip

# http_proxy Settings

Client_max_body_size 10m

Client_body_buffer_size 128k

Proxy_connect_timeout 75

Proxy_send_timeout 75

Proxy_read_timeout 75

Proxy_buffer_size 4k

Proxy_buffers 4 32k

Proxy_busy_buffers_size 64k

Proxy_temp_file_write_size 64k

# load balance Settings

Upstream backend_tomcat {

Server 192.168.100.126 tomcat 8080 weight=1 max_fails=2 fail_timeout=10s; # # needs to be changed to ip of tomcat

Server 192.168.100.127tomcat 8080 weight=1 max_fails=2 fail_timeout=10s; # # needs to be changed to ip of tomcat

}

# virtual host Settings

Server {

Listen 80

Server_name www.benet.com

Charset utf-8

Location / {

Root html

Index index.jsp index.html index.htm

}

Location *\. (jsp | do) ${

Proxy_pass http://backend_tomcat;

Proxy_redirect off

Proxy_set_header Host $host

Proxy_set_header X-Real-IP $remote_addr

Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

Proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504

}

Location / nginx_status {

Stub_status on

Access_log off

Allow 192.168.100.0 allow 24; # # need to change the ip segment of tomcat

Deny all

}

}

}

Restart to make it effective

[root@nginx conf] # / usr/local/sbin/nginx

[root@nginx conf] # service nginx restart

Restarting nginx (via systemctl): [OK]

[root@nginx] # firewall-cmd-- permanent-- add-port=80/tcp

Success

[root@nginx] # firewall-cmd-- reload

Success

These are the basic steps for nginx deployment!

Extend:

In addition to nginx syntax checking whether nginx installation and browsing is normal, there are two common methods:

1. Use the wget command to check

[root@nginx ~] # wget 127.0.0.1

two。 Use the curl command to check

[root@nginx ~] # curl 127.0.0.1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report