nginx一点儿基础配置(一)
1. nginx配置https访问
- 1.1 使用
nginx -V
产看编译参数中是否含有--with-http_ssl_module
,如果没有则继续操作,如果有则跳到1.6步。注意:nginx如果已经安装了其他模块,在安装新模块编译时,必须也要带上其他模块的编译参数,不然会丢弃掉其他模块! 编译参数就是通过nginx -V
查看的! 1.2 安装
OpenSSL
yum -y install openssl openssl-devel
1.3 在nginx的安装包目录下重新编译,注意不是
/usr/local/nginx/
目录下,这是安装目录,不是安装包目录,编译要在安装包目录下进行,当然,安装目录下也没有configure
这个可执行文件。./configure --prefix=/usr/local/nginx --with-http_ssl_module
1.4 继续在安装包目录下执行make命令进行编译,注意为了保险起见,不要执行
make install
这样会重新安装nginx,虽然很多人说执行没有关系,不会覆盖 nginx.conf文件,但是防着点总归没有错。make
1.5 在
make
完之后,先停掉nginx服务。在安装包目录中有一个objs
目录(跟configure
可执行文件平级),将objs
目录下的nginx
文件复制到/usr/local/nginx/sbin/
目录下,覆盖掉其中的nginx
可执行文件(当然,这里重名或者备份之后再删除,做好备份),命令如下:# 将编译完成的 nginx可执行文件 复制到 nginx的安装目录下的 sbin中,覆盖掉其中的nginx cp /nginx安装包目录/objs/nginx /usr/local/nginx/sbin/
1.6 将SSL证书复制到
/usr/local/nginx/conf/
目录中,如下名称:证书名称.pem 证书名称.key
1.7 修改
/usr/local/nginx/conf/nginx.conf
文件server { listen 443 ssl; server_name 证书对应的域名; charset utf-8; ssl_certificate 证书名称.pem; ssl_certificate_key 证书名称.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ...下面是配置 location 内容 }
- 1.8 启动nginx,使用https访问。
1.9 将http请求转为 https请求
# 在请求方式为 http的server中,所有的location上面添加如下代码 rewrite ^(.*) https://$server_name$1 permanent; # 也可以自定义跳转的端口和路径 rewrite ^(.*) http://$server_name:82/manage$1 permanent; # 当post请求比变为get请求时,不要用上面的,用下面这个 return 307 https://$host:82/manage$request_uri;
2. 一个server中配置多个静态资源的location时注意的问题
下面两个是静态资源的location,启动nginx没有问题,访问 https://XXXX/a.png 也没有问题,但是访问 https://XXXX/other/c.jpg 就出现问题了,访问不到。
location / {
root /data/f;
try_files $uri $uri/ /index.html;
}
location /other/ {
root /data/f;
try_files $uri $uri/ /index.html;
}
解决方法:
将 /other/
节点中的 root 改为 alias
,如下:
location / {
root /data/f;
try_files $uri $uri/ /index.html;
}
location /other {
alias /data/f;
try_files $uri $uri/ /index.html;
}
这样就可以正常访问了,如果出现问题,再 /other
后面再加个/
试试。
3. 请求体过大
一般传递文件时,如过文件比较大,则会报请求体过大的错误,在nginx配置扩一下容就行了,在
http节点下添加 client_max_body_size 100m;
如下:
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 100m;
...下面时其他配置和server
}
4.同一个端口保证http跟https同时访问
其实一个端口不可能同时满足http跟https同时访问,只是在http访问的时候进行了强转重定向成https,在server模块中添加错误码497进行重定向
error_page 497 =307 https://$host:84$request_uri;
# 详情可浏览: https://segmentfault.com/q/1010000043433837
解决vue项目在nginx中配置location不是 / 的问题
mahy阅读 318
Nginx常用配置及和基本功能讲解 | 京东云技术团队
京东云开发者赞 5阅读 580
http 和 https 的通信过程及区别
tiny极客赞 2阅读 3k评论 2
有了 NGINX 和 Kong,为什么还需要 Apache APISIX?
API7_技术团队赞 1阅读 4.4k
反向代理学习笔记(一) Nginx与反向代理绪论
北冥有只鱼赞 2阅读 1k
Apache 中 ServerTokens 参数设置详解
danielxue7阅读 3.4k
vue项目部署到阿里云服务器(windows - Nginx代理)
墨城赞 2阅读 761
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。