1. 程式人生 > >nginx虛擬主機和域名跳轉

nginx虛擬主機和域名跳轉

mpi pen inf 提示 etc 主機 表示 nginx.pid 父進程

nginx介紹

nginx官網 :nginx.org
nginx主要應用web服務、反向代理和負載均衡的作用上
nginx分支,淘寶基於nginx開發的Tengine,使用上和nginx一致,服務和配置名一致
nginx比起apache在處理靜態頁面時更有優勢,nginx最大區別在於Tenging支持一些定制化模塊,在安全限速方面比較突出,支持js、css合並,優化web的高並發的訪問需求
nginx核心+lua相關組件和模塊可以組成一個支持lua的高性能web容器openresty,openresty(openr‘s‘t)支持更高性能的web訪問需求

安裝nginx

在官網下載nginx解壓,執行默認的編譯參數,不加其他的參數選項,後面學習需要模塊的話再重新編譯

解壓nginx編譯安裝包,並configure執行編譯

[root@localhost src]# wget http://nginx.org/download/nginx-1.15.2.tar.gz
--2018-08-10 22:48:38-- http://nginx.org/download/nginx-1.15.2.tar.gz
正在解析主機 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
正在連接 nginx.org (nginx.org)|206.251.255.63|:80... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:1025746 (1002K) [application/octet-stream]
正在保存至: “nginx-1.15.2.tar.gz”
100%[====================================================>] 1,025,746 3.43KB/s 用時 4m 34s 
2018-08-10 22:53:12 (3.66 KB/s) - 已保存 “nginx-1.15.2.tar.gz” [1025746/1025746])
[root@localhost src]# tar zxf nginx-1.15.2.tar.gz -C .
[root@localhost src]# cd nginx-1.15.2/
[root@localhost nginx-1.15.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.15.2]# ./configure --prefix=/usr/local/nginx
checking for OS
 + Linux 3.10.0-514.el7.x86_64 x86_64
checking for C compiler ... found
---------------------------------省略過程
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.15.2]# make
make -f objs/Makefile
make[1]: 進入目錄“/usr/local/src/nginx-1.15.2”
[root@localhost nginx-1.15.2]# make install 
------------------------------------省略過程

編譯安裝後編寫nginx的啟動腳本文件,這個文件存放在/etc/init.d/目錄下,給予權限755,我這裏想給nginx添加到系統啟動中,但是chkconfig提示不支持此操作,這是因為在啟動腳本前需要指定配置chkconfig的默認的一個運行級別,否則chkconfig時就會提示不支持此操作

[root@localhost conf]# 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
[root@localhost nginx-1.15.2]# chkconfig --add nginx

服務 nginx 不支持 chkconfig ? ?---------錯誤提示,需要在啟動配置文件前添加chkconfig運行級別配置

[root@localhost nginx-1.15.2]# chmod 755 /etc/init.d/nginx 
[root@localhost conf]# chkconfig --add nginx
[root@localhost conf]# chkconfig nginx on
[root@localhost conf]# chkconfig --list
mysqld ? ? ? ?  0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole ? ?  0:關 1:關 2:關 3:關 4:關 5:關 6:關
network ? ? ?   0:關 1:關 2:開 3:開 4:開 5:開 6:關
nginx ? ? ? ?   0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm ? ? ?   0:關 1:關 2:開 3:開 4:開 5:開 6:關

chkconfig添加系統自啟動後無報錯,再配置nginx.conf的配置文件,這裏簡單描述下其參數功能,其功能如下:

[root@localhost conf]# vim 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;
 ? ? ?  } ? ?
 ?  }
}
nginx的全局配置
user nobody nobody; ? ? ? 指定nginx進程的運行用戶
worker_processes 2; ? ? ?  一個父進程開啟的子進程
error_log /usr/local/nginx/logs/nginx_error.log crit; ? ? 日誌存放的目錄
pid /usr/local/nginx/logs/nginx.pid; ? ? ? ? ?  啟動停止時更新的pid文件
worker_rlimit_nofile 51200; ? ? ? ? ?  每個進程最大打開文件的數量

使用ps -aux查看nginx進程時會看到一個Ss的父進程,父進程是由root用戶運行,其子進程是由服務的服務用戶運行的,如nobody用戶

server部分配置

server
 ?  {
 ? ? ?  listen 80; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  nginx的啟動端口
 ? ? ?  server_name localhost; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  nginx本地解析名稱,這裏填寫網站域名
 ? ? ?  index index.html index.htm index.php; ? ? ? ? ? ? 指定默認的網站頁面
 ? ? ?  root /usr/local/nginx/html; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 指定網站根目錄

 ? ? ?  location ~ \.php$ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  php解析模塊配置
 ? ? ?  {
 ? ? ? ? ?  include fastcgi_params; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  php的工作模式
 ? ? ? ? ?  fastcgi_pass unix:/tmp/php-fcgi.sock; ? ? ? ? ? nginx的通信類型,另一種類型是127.0.0.1:9000;的回環地址解析的方式
 ? ? ? ? ?  fastcgi_index index.php; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? php解析的首頁
 ? ? ? ? ?  fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
 ? ? ?  } ? ?
 ?  }
}

nginx默認虛擬主機

nginx配置默認虛擬主機除了server模塊方式,還有指定虛擬主機配置文件這個方式,指定單獨的虛擬主機配置文件,一個配置文件就是一個虛擬主機,這裏只簡單配置一個虛擬主機,不涉及域名和其他配置
在nginx.conf配置文件中加入指定虛擬主機配置文件的配置路徑,這裏只需要打開虛擬主機配置文件即可,添加虛擬主機配置文件必須把nginx.conf配置文件中的server配置模塊刪除掉,否則虛擬主機配置文件則不會生效

[root@localhost conf]# vim nginx.conf
 ?  application/xml;
 ?  include vhost/*.conf;
 ?  server

配置默認虛擬主機文件,默認虛擬主機文件是保存在vhost目錄下的。vhost在conf目錄下,默認是不存在的,需要手動創建,默認虛擬主機的根網站路徑也是不存在的,需要手動創建

[root@localhost conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.1 scgi_params.default win-utf
fastcgi.conf.default koi-utf mime.types.default nginx.conf.default uwsgi_params
fastcgi_params koi-win nginx.conf scgi_params uwsgi_params.default
[root@localhost conf]# mkdir vhost
[root@localhost conf]# cd vhost/
[root@localhost vhost]# vim a.nginx.conf
server
{
 ? listen 80 default_server;
 ? server_name aaa.com;
 ? index index.html index.htm index.php;
 ? root /data/wwwroot/aaa;
}

編輯虛擬主機配置文件中指定的網站根目錄的頁面進行測試,-s reload是重新加載配置文件,不用重啟nginx服務,這裏沒有域名,所以測試訪問是在本機訪問回環地址進行測試的,配置域名解析後即可公網測試訪問

[root@localhost vhost]# mkdir -p /data/wwwroot/aaa
[root@localhost vhost]# vim /data/wwwroot/aaa/index.php
<?php
phpinfo();
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
-------------------------測試url訪問
[root@localhost conf]# curl localhost
This is the default site.
[root@localhost conf]# curl -x127.0.0.1:80 aaa.com
This is the default site.

nginx用戶認證

在conf/vhost/目錄下編輯一個虛擬主機配置文件,這裏定義新虛擬主機配置文件為b.conf

[root@localhost vhost]# vim b.conf 
server
{
 ? listen 80 ;
 ? server_name b.com;
 ? index index.html index.htm index.php;
 ? root /data/wwwroot/b;
 ? 
 ? 
 ? location /
 ? {
 ? ? ? ? ? auth_basic "Auth";
 ? ? ? ? ? auth_basic_user_file /usr/local/nginx/conf/htpasswd;
 ? }
}

指定用戶認證需要使用到apache的htpasswd的密碼生成工具,這裏可以使用yum安裝httpd,在使用完成後也可刪除,建議保留,方便日後再次添加認證用戶

[root@localhost vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd user
New password: 
Re-type new password: 
Adding password for user user
[root@localhost vhost]# htpasswd /usr/local/nginx/conf/htpasswd user1 ? ? ?  再次添加一個用戶
New password: 
Re-type new password: 
Adding password for user user1
[root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd 
user:$apr1$AUrl5dQq$GpglCih5wADphsN7KJ0LQ1
user1:$apr1$Nfc9PosN$OHQFumTtuYxb3.LR4v72J1

測試下訪問用戶認證,401錯誤碼表示頁面限制訪問,curl -u指定用戶認證訪問測試

[root@localhost vhost]# curl -x127.0.0.1:80 b.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.15.2</center>
</body>
</html>
[root@localhost vhost]# curl -uuser1:pwd@321 -x127.0.0.1:80 b.com
bbb

設置頁面、目錄用戶認證訪問,location(lou;‘k;‘sèng)模塊後配置匹配限制訪問的目錄或頁面

[root@localhost vhost]# vim b.conf 
server
{
 ? listen 80;
 ? server_name b.com;
 ? index index.html index.htm index.php;
 ? root /data/wwwroot/b;
 ? location ?  ~  admin.php ? ? ? ? ? ? ? ? ? ? ? ? ?  匹配規則: ? ? ?  ~ admin.php ? 或者匹配目錄 ? /admin/
 ? {
 ? ? ? ? ? auth_basic "Auth";
 ? ? ? ? ? auth_basic_user_file /usr/local/nginx/conf/htpasswd;
 ? }
}

nginx域名重定向

nginx域名重定向比httpd域名重定向配置起來要簡單很多,其匹配域名也能寫多個,httpd中配置域名跳轉只能匹配一個域名,針對一個域名進行跳轉,nginx的server_name後可以跟多個域名進行匹配,當有訪問到匹配的域名時,nginx就會把訪問跳轉至rewrite指定跳轉的域名上,配置及測試如下

[root@localhost vhost]# vim aaa.conf 
server
{
 ? listen 80 default_server;
 ? server_name ddd.com ccc.com;
 ? server_name aaa.com;
 ? index index.html index.htm index.php;
 ? root /data/wwwroot/aaa;
 ? if ($host != ‘test.com‘)  {
 ? ? ? ?  rewrite ^/(.*)$ http://aaa.com/$1  permanent;
 ? }
}

重新加載配置文件並測試ccc.com域名跳轉

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x127.0.0.1:80 ccc.com/index.php -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.2
Date: Fri, 10 Aug 2018 19:03:20 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://aaa.com/index.php

測試ddd.com域名跳轉

[root@localhost vhost]# curl -x127.0.0.1:80 ddd.com/index.php -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.2
Date: Fri, 10 Aug 2018 19:03:43 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://aaa.com/index.php

nginx虛擬主機和域名跳轉