2019-03-26 14:11:56 1454浏览
今天扣丁学堂Linux培训老师给大家介绍一下关于NginxWeb服务器保驾护航的若干要点,首先Nginx是全球发展势头最猛的开源轻量级高性能Web服务器系统。Nginx可在Linux、Windows、MacOS和Solaris等操作系统上运行。Nginx继续人气激增,意味着越来越多的Nginx部署环境需要加以保护。
apt-get install nginx -y
systemctl status nginx
?nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2019-03-10 02:43:14 UTC; 4min 40s ago Docs: man:nginx(8) Process: 2271 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 2281 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 2274 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 2285 (nginx) Tasks: 2 (limit: 1111) CGroup: /system.slice/nginx.service ??2285 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ??2290 nginx: worker process Mar 10 02:43:14 ubuntu1804 systemd[1]: Starting A high performance web server and a reverse proxy server... Mar 10 02:43:14 ubuntu1804 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument Mar 10 02:43:14 ubuntu1804 systemd[1]: Started A high performance web server and a reverse proxy server.
apt-get update -y apt-get install nginx --reinstall -y
curl -I http://localhost
HTTP/1.1 200 OK Server: nginx/1.14.0 (Ubuntu) Date: Sat, 09 Mar 2019 15:28:01 GMT Content-Type: text/html Content-Length: 10918 Last-Modified: Fri, 01 Feb 2019 16:05:17 GMT Connection: keep-alive ETag: "5c546e3d-2aa6" Accept-Ranges: bytes
nano /etc/nginx/nginx.conf
http { ## # Basic Settings ## server_tokens off;
systemctl restart nginx
curl -I http://localhost
HTTP/1.1 200 OK Server: nginx Date: Sat, 09 Mar 2019 15:33:31 GMT Content-Type: text/html Content-Length: 10918 Last-Modified: Fri, 01 Feb 2019 16:05:17 GMT Connection: keep-alive ETag: "5c546e3d-2aa6" Accept-Ranges: bytes
nano /etc/nginx/sites-enabled/default
server { listen 80 default_server; listen [::]:80 default_server; allow 172.16.0.0/16; deny all;
systemctl restart nginx
tail -f /var/log/nginx/error.log
2019/03/0916:13:01[error]11589#11589:*1accessforbiddenbyrule,client:192.168.0.102,server:_,request:"GET/test/HTTP/1.1",host:"172.16.0.122"
mkdir /etc/nginx/ssl/
cd /etc/nginx/ssl/
openssl genrsa -aes256 -out nginx.key 1024
Generating RSA private key, 1024 bit long modulus ...++++++ .............................++++++ e is 65537 (0x010001) Enter pass phrase for nginx.key: Verifying - Enter pass phrase for nginx.key:
openssl req -new -key nginx.key -out nginx.csr
Generating RSA private key, 1024 bit long modulus ...++++++ .............................++++++ e is 65537 (0x010001) Enter pass phrase for nginx.key: Verifying - Enter pass phrase for nginx.key: root@ubuntu1804:~# openssl req -new -key nginx.key -out nginx.csr Enter pass phrase for nginx.key: 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) [AU]:IN State or Province Name (full name) [Some-State]:Gujarat Locality Name (eg, city) []:Junagadh Organization Name (eg, company) [Internet Widgits Pty Ltd]:IT Organizational Unit Name (eg, section) []:IT Common Name (e.g. server FQDN or YOUR name) []:HITESH Email Address []:admin@example.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:admin An optional company name []:IT
openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt
Signature ok subject=C = IN, ST = Gujarat, L = Junagadh, O = IT, OU = IT, CN = HITESH, emailAddress = admin@example.com Getting Private key Enter pass phrase for nginx.key:
nano /etc/nginx/sites-enabled/default
server { listen 192.168.0.100:443 ssl; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
systemctl restart nginx
mkdir /etc/nginx/.htpasswd htpasswd -c /etc/nginx/.htpasswd/passwd admin
New password: Re-type new password: Adding password for user admin
mkdir /var/www/html/test
chown -R www-data:www-data /var/www/html/test
nano /etc/nginx/sites-enabled/default
location /test { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd/passwd;
systemctl restart nginx
接下来打开Web浏览器,输入URLhttp://your-server-ip/test。系统会提示你输入用户名和密码,访问测试目录,如下所示:
【关注微信公众号获取更多学习资料】 【扫码进入HTML5进阶免费公开课】