Twelfth two-week Nginx installation, Nginx default virtual host, Nginx user authentication, Nginx domain name redirection

Nginx installation

First enter the /usr/local/src directory. Then download Nginx.
wget http://nginx.org/download/nginx-1.12.1.tar.gz
then decompress
tar zxf nginx-1.12.1.tar.gz
and then enter the directory we just decompressed to compile
cd nginx-1.12.1
[ root@linletao-001 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
There are no compilation parameters here. We can add parameters in the later compilation according to the actual situation.
When we install the environment in the future, the source package must be retained, because we do not necessarily need any modules, so it is necessary to retain the source package.
After compiling, we install
make && make install
After installation, we will get four directories
[root@linletao-001 nginx-1.12.1]# ls /usr/local/nginx/
conf html logs sbin
Then we edit a startup script
vim /etc/init.d/nginx
#!/bin/bash

chkconfig: - 30 21

description: http service.

Source Function Library

. /etc/init.d/functions

Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
can also be copied directly from the website, https://coding.net/u /aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx
then change the permissions of the start script
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
boots up and starts
chkconfig nginx on
Then we change the nginx configuration file, we need to clear the original configuration file.
First we enter the nginx configuration file directory
cd /usr/local/nginx/conf/
and then clear the previous configuration file

/usr/local/nginx/conf/nginx.conf (> redirection symbol, used alone, you can quickly clear a document.)
Then edit the configuration file
vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types ;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/ nginx/html;
location ~ .php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
can also go to the website Copy https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf directly
and check for syntax errors
[root@linletao-001 conf]# /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 The
configuration syntax is correct.
Then we start nginx
/etc/init.d/nginx start
to see if it starts successfully
[root@linletao-001 logs]# ps aux |grep nginx
root 5213 0.0 0.0 20496 620 ? Ss 22:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 5214 0.0 0.3 22940 3208 ? S 22:10 0:00 nginx: worker process
nobody 5215 0.0 0.3 22940 3208 ? S 22:10 0:00 nginx: worker process
root 5219 0.0 0.0 112676 976 pts/0 R+ 22:10 0:00 grep --color=auto nginx
shows two workers above, indicating that the startup is successful. Then we check the port
[root@linletao-001 logs]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5213/nginx:
The master has successfully listened to port 80.
Then we see if we can parse php
first edit the file
vim /usr/local/nginx/html/1.php
and then enter the content
<?php
echo "this is nginx pape.";
Finally, use the curl command to parse
[root@linletao-001 logs ]# curl localhost/1.php
this is nginx pape.[root@linletao-001 logs]#The
parsing is successful.

Nginx default virtual host

First, change the configuration file
vim /usr/local/nginx/conf/nginx.conf of nginx.conf
and then delete all the content below service in the configuration file.
It should be noted here that } is reserved, leaving only the last one.
Then add a line of text at the end
and enter /usr/local/nginx/conf
to add a subdirectory vhost
and enter this subdirectory, create a file such as aaa.com.conf
and copy the following content to the newly created file.
server
{ listen 80 default_server;
server_name aaa.com; index index.html index.htm index.php;
root /data/wwwroot/default;
}
save and exit
then create mkdir -p /data/wwwroot/default/
and enter the following
echo “This is a default site.”>/data/wwwroot/default/index.html
then check for syntax errors
/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
and then create the browser's root directory
mkdir -p /data/wwwroot/default/
Then we define a file
vim index.html
enter a content
This is the default site.
Check for syntax errors after saving
root@linletao-001 default]# /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
If the syntax is correct, we will reload
/usr/local/nginx/sbin/nginx -s reload
and test
[root@linletao-001 default ]# curl localhost
This is the default site.
[root@linletao-001 default]# curl -x127.0.0.1:80 123.com
This is the default site.
[root@linletao-001 default]# curl -x127.0.0.1:80 bbb.com
This is the default site. The
test is successful.

Nginx user authentication

First, let's create a virtual host
vim /usr/local/nginx/conf/vhost/test.com.conf
and enter the following content.
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic "Auth"; (define user name)
auth_basic_user_file /usr/local/ nginx/conf/htpasswd; (username and password file)
}
}
We can copy it directly at work later, but pay attention to the difference between server_name and root.
Then we generate the username password file. If we have apache, we can directly use its password to generate the file. If not, we can install a apaceh with yum first, and then we can use it.
htpasswd -c /usr/local/nginx/conf/htpasswd aming (-c means generation. If you want the second one, you don't need to add -c)
Here we send the user password to the aming file.
Then we still go to check for syntax errors and reload
/usr/local/nginx/sbin/nginx -t




















mkdir /data/wwwroot/test.com
echo “test.com” > /data/wwwroot/test.com/index.html
and then we visit again.
[root@linletao-001 vhost]# curl -uaming:19860127 -x127.0.0.1:80 test.com
"test.com"
is normal.
There is also a situation where we need to authenticate when we need to access a directory. For example, we need authentication when we want him to access admin, then we can do this
in the configuration file vim /usr/local/nginx/conf/vhost/test.com.conf The location / in the file is directly connected to admin, and the location / admin can be changed directly to the directory name.
This way we don't have to enter a password when we visit test.com. To access admin, you need to enter a password. Otherwise, a 401 will be reported.

Nginx domain name redirection

Nginx's domain name redirection is similar to httpd. First, let's change the configuration file
vim /usr/local/nginx/conf/vhost/test.com.conf
and then add some domain names
server_name test.com test2.com test3.com after server_name,
but the weight will change, in order to To prevent the weight from changing, then we need to add a line to the configuration file to configure
if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent;
its It means that if the domain name is not test.com, I have to make a jump.
Then we do a test
[root@linletao-001 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 24 Apr 2018 16:08:27 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
So no matter what kind of domain name we enter, he will jump Go to tes.com, our pre-configured.
But if we enter an unknown domain name, it will access our default virtual host

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810039&siteId=291194637