Q1、基于location实现基于域名虚拟主机
[root@Centos7 ~]# cd /apps/nginx/conf.d/
[root@Centos7 conf.d]# vim web.conf
server_tokens off;
server {
listen 192.168.37.87:80;
server_name test.magedu.net;
location / {
root /data/site1;
index index.html;
}
}
[root@Centos7 conf.d]# echo test > /data/site1/index.html
[root@Centos7 conf.d]# nginx
#客户端测试
[root@centos6 ~]$ curl test.magedu.net
test
测试结果:
Q2、nginx自定义日志路径,配置访问日志为json格式
[root@Centos7 conf.d]# vim ../conf/nginx.conf
http {
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
}
[root@Centos7 conf.d]# vim web.conf
server_tokens off;
server {
listen 192.168.37.87:80;
server_name test.magedu.net;
access_log logs/test.magedu.net.access.log json;
location / {
root /data/site1;
index index.html;
}
}
[root@Centos7 conf.d]# nginx -s reload
#修改后的日志
[root@Centos7 conf.d]# tail /apps/nginx/logs/test.magedu.net.access.log
192.168.37.1 - - [29/Jul/2020:20:43:47 +0800] "GET / HTTP/1.1" 200 5 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
192.168.37.1 - - [29/Jul/2020:20:43:47 +0800] "GET /favicon.ico HTTP/1.1" 404 548 "http://test.magedu.net/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
192.168.37.6 - - [29/Jul/2020:20:46:05 +0800] "GET / HTTP/1.1" 200 5 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
{"@timestamp":"2020-07-29T20:53:06+08:00","host":"192.168.37.87","clientip":"192.168.37.6","size":5,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"test.magedu.net","uri":"/index.html","domain":"test.magedu.net","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2","status":"200"}
{"@timestamp":"2020-07-29T20:53:07+08:00","host":"192.168.37.87","clientip":"192.168.37.6","size":5,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"test.magedu.net","uri":"/index.html","domain":"test.magedu.net","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2","status":"200"}
Q3、nginx实现https证书
1、生成CA证书
[root@Centos7 conf.d]# cd /etc/pki/tls/certs/
[root@Centos7 certs]# vim Makefile
57 #/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
58 /usr/bin/openssl genrsa $(KEYLEN) > $@
[root@Centos7 certs]# make magedu.net.crt
umask 77 ; \
#/usr/bin/openssl genrsa -aes128 2048 > magedu.net.key
/usr/bin/openssl genrsa 2048 > magedu.net.key
Generating RSA private key, 2048 bit long modulus
.......................................................+++
..........................................+++
e is 65537 (0x10001)
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key magedu.net.key -x509 -days 365 -out magedu.net.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GuangDong
Locality Name (eg, city) [Default City]:GuangZhou
Organization Name (eg, company) [Default Company Ltd]:magedu.net
Organizational Unit Name (eg, section) []:opt
Common Name (eg, your name or your server's hostname) []:test.magedu.net
Email Address []:
[root@Centos7 certs]# mkdir /apps/nginx/ssl/
[root@Centos7 certs]# mv magedu.net.* /apps/nginx/ssl/
[root@Centos7 certs]# vim /apps/nginx/conf.d/web.conf
server_tokens off;
server {
listen 192.168.37.87:80;
listen 192.168.37.87:443 ssl;
server_name test.magedu.net;
access_log logs/test.magedu.net.access.log json;
ssl_certificate /apps/nginx/ssl/magedu.net.crt;
ssl_certificate_key /apps/nginx/ssl/magedu.net.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location / {
root /data/site1;
index index.html;
if ( $scheme = http ) {
rewrite ^/(.*)$ https://test.magedu.net/$1 permanent;
}
}
}
[root@Centos7 certs]# nginx -s reload
#客户端测试
[root@centos6 ~]$ curl test.magedu.net
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@centos6 ~]$ curl -kL test.magedu.net
test