转载原文地址:https://www.cnblogs.com/hailang8/p/8664413.html
1、安装:yum -y install nginx
最后推荐一个牛皮的Nginx配置网站,看懂并使用起来需要有一定的基础。
nginxconfig.io
最后贴一下我的Nginx.conf通用配置模板
# Generated by nginxconfig.io
# https://nginxconfig.io/?0.domain=_&0.path=%2Froot%2Fnginx&0.document_root=%2Fhtml&0.redirect=false&0.https=false&0.php=false&0.index=index.html&0.fallback_html&0.access_log_domain&0.error_log_domain&user=root&file_structure=unified
user root;#centos用户,看需修改
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
multi_accept on;
worker_connections 65535;
}
http {
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
client_max_body_size 16M;
# MIME
include mime.types;
default_type application/octet-stream;
# logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
# load configs
include /etc/nginx/conf.d/*.conf;
# _
server {
listen 80;
listen [::]:80;
server_name _;
root /root/nginx/html;#html页面根目录,看需修改
# security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
# . files
location ~ /\.(?!well-known) {
deny all;
}
# logging
access_log /var/log/nginx/_.access.log;
error_log /var/log/nginx/_.error.log warn;
# index.html fallback
location / {
try_files $uri $uri/ /index.html;
}
# favicon.ico
location = /favicon.ico {
log_not_found off;
access_log off;
}
# robots.txt
location = /robots.txt {
log_not_found off;
access_log off;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
}
}