一.nginx安装
注:需先安装一下应用
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
a)下载对应nginx离线安装包
b)解压对应安装包后到对应目录下执行一下命令
./configure --prefix=/usr/local/nginx --with-http_ssl_module
注:(prefix目录是应用安装目录)
二.nginx启动
a)启动
nginx -c nginx.conf
b)停止
nginx -s stop
c)重启
nginx -s reopen;
d)重新加载配置文件
nginx -s reload
三.nignx代理
sever内配置
目录代理
location / {
alias /home/wzfxft/dist/;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
网址代理
location /dzFile {
proxy_pass http://10.36.198.94:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
四种代理路径
例如:http://192.168.0.105:8087/api/system/common/doLogin
1)
请求nginx: http://192.168.0.105:8087/api/system/common/doLogin
Nginx代理替换之后: http://192.168.0.105:8089/api/system/common/doLogin
2)
请求nginx: http://192.168.0.105:8087/api/system/common/doLogin
Nginx代理替换之后: http://192.168.0.105:8089/ /system/common/doLogin
3)
请求nginx: http://192.168.0.105:8087/api/system/common/doLogin
Nginx代理替换之后: http://192.168.0.105:8089/api/system/common/doLogin
4)
请求nginx: http://192.168.0.105:8087/api/system/common/doLogin
Nginx代理替换之后: http://192.168.0.105:8089/system/common/doLogin
根据上述四种情况总结,归结为两种代理过程:
(1).如果代理proxy_pass只有ip和port,没有上下文,则代理过程只替换掉nginx请求的ip和port,其他部分都不变,然后发送。
(2).如果代理proxy_pass除了ip和port外,还有上下文,则代理过程中,除了替换ip和port之外,还会将location的路径替换为proxy_pass中的上下文, 然后将剩余的路径拼接,发送。
配置证书
server内配置
ssl_certificate "/home/ssl/wz/wzfxft.cer";
ssl_certificate_key "/home/ssl/wz/wzfxft-unsecure.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
四.其他
隐藏版本号有两种方式
1)修改Nginx的主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
........... #省略内容
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #关闭版本号
............ #省略内容
2)Nginx的源码文件包含了版本信息,可以随意设置,然后重新编译安装,就会隐藏版本信息。
[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h #编辑源码文件
#define NGINX_VERSION "1.1.1" #修改版本号
#define NGINX_VER "IIS" NGINX_VERSION #修改服务器类型
重新编译安装
[root@localhost ~]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
413 Request Entity Too Large 代理上传文件太大
可以选择在http{ }中设置:client_max_body_size 20m;
也可以选择在server{ }中设置:client_max_body_size 20m;
还可以选择在location{ }中设置:client_max_body_size 20m;
配置后需重启nginx
SSL received a record that exceeded the maximum permissible length ERR_SSL_PROTOCOL_ERROR错误解决方法
修改server里面的 listen 443; 为 listen 443 default ssl; 保存,重启Nginx。