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

Nginx access control and virtual host (port-based, domain name-based) with source code included

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The nginx installation file can be downloaded from the official website http://www.nginx.org/. The following takes the stable version of nginx-1.6.0 as an example to introduce the installation and operation control of nginx. The experimental host is the redhat6.5 system.

Free source code package: Baidu cloud disk

Https://pan.baidu.com/s/1nQ9Qok49au7Cn3elS7IbBQ1, compilation and installation nginx (1) installing the configuration and operation of the supporting software nginx requires the support of pcre,zlib and other software packages, so the development packages pcre-devel and zlib-devel of these software should be pre-installed in order to provide the corresponding libraries and header files to ensure the smooth installation of nginx

Yum-y install pcre-devel zlib-devel gcc gcc-c++

(2) decompress the nginx software package and create users and groups. Nginx service programs run as nobody by default. It is recommended to create special user accounts for them in order to control their access rights more accurately, increase flexibility, and reduce security risks. For example, create a user named nginx without establishing a host directory and forbidding login to the shell environment.

Tar xzvf nginx-1.6.0.tar.gz-C / opt

Cd / opt/nginx-1.6.0/

Useradd-M-s / sbin/nologin nginx

(3) compile and install nginx

. / configure\

-- prefix=/usr/local/nginx\

-- user=nginx\

-- group=nginx\

-- with-http_stub_status_module / / enable stub_status status statistics module / /

Make

Make install

Ln-s / usr/local/nginx/sbin/* / usr/local/sbin/ establish a soft connection and let the system recognize the nginx command

(4) Operation control of nginx

-check, start, restart, stop-

Nginx-t / / check

Nginx / / start

Killall-1 nginx / / restart

Killall-3 nginx / / stop

-production of management scripts-

Vi / etc/init.d/nginx

#! / bin/bash

# chkconfig:-99 20

# description: Nginx Service Control Script

PROG= "/ usr/local/nginx/sbin/nginx"

PIDF= "/ usr/local/nginx/logs/nginx.pid"

Case "$1" in

Start)

$PROG

Stop)

Kill-s QUIT $(cat $PIDF)

Restart)

$0 stop

$0 start

Reload)

Kill-s HUP $(cat $PIDF)

*)

Echo "Usage: $0 {start | stop | restart | reload}"

Exit 1

Esac

Exit 0

Chmod + x / etc/init.d/nginx

Chkconfig-add nginx

2.nginx access status statistics nginx has a built-in http_stub_status status statistics module, which is used to feedback the current web access. To use the nginx statistics function, in addition to enabling the built-in module, you need to modify the nginx.conf configuration file, specify the access location and add the stub_status configuration code.

[root@ling conf] # vim nginx.conf

Server {

Listen 80

Server_name localhost

Charset utf-8

Location / {

Root html

Index index.html index.htm

}

Location ~ / status {

Stub_status on

Access_log off

} / / insert these four lines here in "server"

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root html

}

}

}

-the explanation is as follows-

Location ~ / status {/ / access location is / status

Stub_status on; / / turn on the status statistics function

Access_log off; / / turn off logging at this location

}

Then restart the nginx service. After the new configuration takes effect, you can visit the / status site location of the nginx server in the browser to see the current status statistics.

Active connettions / / indicates the current number of active connections server accepts handle requests / / indicates the connection information that has been processed: the number of connections processed; the number of successful TCP handshakes; and the number of requests processed

3. Virtual hosts based on domain names make use of virtual hosts. Instead of providing a separate nginx server or running a group of nginx processes for each website to be run, virtual hosts provide the function of running multiple websites on the same server and the same group of nginx processes. When using nginx to build a virtual host server, each virtual web site has a separate "server {}" configuration segment, its own listening ip address, port number, and website name. (1) modify the configuration file nginx.conf, remove all the server {} code segments in the configuration file, and add two new server {} segments, corresponding to the two domain names accp.com and benet.com.

(2) prepare the site directory and test home page of each website respectively.

(3) in the browser, visit the two domain names respectively, and you can see different pages.

4, the port-based virtual host (1) adds another server {} segment to the configuration file nginx.conf, with the port changed to 8080, corresponding to the same ip address as benet.com.

(2) create site directory and test page

(3) access www.benet.com:8080 port

5. Access control of authorized users nginx, like apache, can achieve access control based on user authorization. When the client wants to visit the corresponding website or directory, the user is required to enter the user name and password to access it normally. (1) use htpasswd to generate user authentication files

(2) the permission to modify the password file is 400, the owner is changed to nginx, and the running user of nginx can read it.

(3) modify the main configuration file and add corresponding configuration items

Location / {

Auth_basic "secret"

Auth_basic_user_file / usr/local/nginx/passwd.db

Root html

Index index.html index.htm

}

(4) visit the web site with a browser to verify the control effect.

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