跨域、
gzip、
SSL、
反向代理、
Nginx实现前后端分离、
HTTP重定向HTTPS
拿来即用,基本全部都有简单注解。
配置实现效果访问http://www.baidu.com 自动跳 https://www.baidu.com、
反向代理 www.baidu.com/8080 为 www.baidu.com/api、
gzip网页压缩、
自定义4040错误页面等。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# # # # # # # # # # # # #
#
# 重点
#
# # # # # # # # # # # # #
server {
# 80端口
listen 80;
# 域名
server_name www.baidu.com;
# 将所有HTTP请求通过rewrite指令重定向到HTTPS。
# 开启则访问Http://www.baidu.com 会自动变Https://www.baidu.com
# rewrite ^(.*)$ https://$host$1;
# gzip压缩
gzip on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 100;
gzip_types application/javascript text/css text/xml;
gzip_disable "MSIE [1-6].";
gzip_vary on;
#rewrite ^/(.*) https://$server_name$request_uri? permanent;
#charset koi8-r;
#access_log logs/host.access.log main;
# # # # # # # # # # # # #
#
# 前后分离
#
# # # # # # # # # # # # #
# 访问www.baidu.com执行
location / {
# 文件所在服务器目录,一般为前端文件
root /xxx/xxx;
index index.html index.htm;
# 解决刷新页面变成404问题
try_files $uri $uri/ /index.html;
# rewrite ^(.*)$ https://$host$1;
}
# 访问 www.baidu.com/api 执行,一般是后端运行端口
location ^~/api/ {
# 实际后台服务器地址,此地址就是http的,可以实现https转发http
proxy_pass http://127.0.0.1:8080;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# 访问 www.baidu.com/404 错误执行
error_page 500 502 503 504 /50x.html;
location = /50x.html {
#相关定制404页面所在目录
root /xxx/xxx;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param script_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
# # # # # # # # # # # # #
#
# HTTPS 重点
#
# # # # # # # # # # # # #
server {
# SSL为443端口,使用阿里云注意去安全组开放443端口
listen 443 ssl;
# 配置HTTPS的默认访问端口为443。
# 如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
# 如果使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
# 域名
server_name www.baidu.com;
# 文件所在服务器目录,一般为前端文件
root /xxx/xxx;
index index.html index.htm;
ssl on;
# 密钥文件目录,需要将cert-file-name.pem替换成已上传的证书文件的名称。
ssl_certificate /xxx/xxx/xxx/597_www.baidu.com.pem;
# 密钥文件目录,需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
ssl_certificate_key /xxx/xxx/xxx/597_www.baidu.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
# 表示使用的加密套件的类型。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
ssl_prefer_server_ciphers on;
# gzip压缩
gzip on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 100;
gzip_types application/javascript text/css text/xml;
gzip_disable "MSIE [1-6].";
gzip_vary on;
location / {
# 前端文件目录
root /xxx/xxx;
index index.html index.htm;
# 解决刷新页面变成404问题的代码
try_files $uri $uri/ /index.html;
# 反向代理相关配置,具体自查
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ^~/api/ {
#实际后台服务器地址www.baidu.com/api,此地址就是http的,可以实现https转发http
proxy_pass http://127.0.0.1:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
# 自定义 404
root /home/xxx;
}
}
}



