nginx 404、日志格式、https配置

yum install nginx -y
systemctl start nginx
systemctl enable nginx
ps -ef |grep nginx

nginx -t 检查nginx配置文件格式是否正确
nginx -s reload 重新加载配置,不用重启nginx进程

nginx静态资源存放路径:
/usr/share/nginx/html/404.html 50x.html index.html

nginx配置文件存放路径:
/etc/nginx/nginx.conf

404错误配置

当访问一个nginx 中不存在的路径时,就会报404错误
例如:
nginx.conf配置404的配置
nginx  404、日志格式、https配置

当访问http://192.168.115.128/ya 时,会出现以下错误页面
nginx  404、日志格式、https配置

json格式输出日志

配置:

# log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
   #                   ‘$status $body_bytes_sent "$http_referer" ‘
   #                   ‘"$http_user_agent" "$http_x_forwarded_for"‘;

     log_format json ‘{"remote_ip":"$remote_addr",‘
                     ‘"remote_user":"$remote_user",‘
                     ‘"time_local":"$time_local",‘
                     ‘"request":"$request",‘
                     ‘"status":"$status",‘
                     ‘"body_sent":"$body_bytes_sent",‘
                     ‘"referer":"$http_referer",‘
                     ‘"agent":"$http_user_agent",‘
                     ‘"forward":"$http_x_forwarded_for"}‘;

   # access_log  /var/log/nginx/access.log  main;
    access_log  /var/log/nginx/access.log  json;

日志输出显示:
nginx  404、日志格式、https配置

HTTPS

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.ilinux.com;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            rewrite (.*) https://www.ilinux.com$1 permanent;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

server {
       listen          443 ssl;
       ssl_protocols   TLSv1.1 TLSv1.2;
       ssl_ciphers     HIGH:!aNULL:!MD5;
       ssl_certificate /etc/nginx/certs/nginx.crt;
       ssl_certificate_key /etc/nginx/certs/nginx.key;
       ssl_session_cache shared:SSL:10m;
       ssl_session_timeout 10m;

       location / {

       }

nginx.key获取方式:openssl genrsa -out /etc/nginx/certs/nginx.key 2048
nginx.crt 获取方式:openssl req -new -x509 -key nginx.key -out nginx.cert -days 3650 -subj "/CN=www.ilinux.com"

当访问www.ilinux.com时,就会跳转到https

相关推荐