其他配置
nginx自动生成目录首页
location下面的配置 autoindex on 可以映射文件目录
nginx黑白名单
可以指定ip指定资源是否能被访问,被拒绝的显示403 Forbidden
配置例子
nginx文件包含
nginx.conf文件
被包含文件
vhost/order.lomi.com.conf
server { #一个server就是一个虚拟主机
listen 80; #虚拟主机端口
server_name localhost; #IP或者域名
#charset koi8-r;
#access_log logs/host.access.log main; #每个虚拟主机可以独立设置日志位置
location / { #location里面是虚拟主机映射的位置
root html; #根目录文件夹
index index.html index.htm; #首页
deny all; #拒绝所有IP
allow 192.168.1.6 #只允许192.168.1.6
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #指定状态吗对应页面
location = /50x.html { #精确匹配50x.html资源位置
root html;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
nginx设置http证书
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem; #指定https证书
ssl_certificate_key cert.key; #指定https证书key
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
nginx日志文件位置
#user nobody;
worker_processes 1; #一般是1,可以和线程数量一样
#error_log logs/error.log; #异常日志位置
events {
worker_connections 1024; #单个work-process允许的最大连接数
}
http {
include mime.types; #这里包含的types块也可以包含http下面的块比如server等
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; #全局访问日志
server { #一个server就是一个虚拟主机
listen 80; #虚拟主机端口
server_name localhost; #IP或者域名
access_log logs/host.access.log main; #每个虚拟主机可以独立设置的访问日志
location / { #location里面是虚拟主机映射的位置
.
.
.
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
nginx代理前端页面跨域问题(CORS)
出现原因是前后端分离以后前端页面在一个域名A,然后后端接口放在另外一个域名B下面
- nginx解决方案1:ngixn接受域名A和域名B的请求,然后分别转发给前端页面和后端接口服务器,这时候浏览器请求的都是nginx服务对应的域名,浏览器不会识别为跨域。
- nginx决绝方案2:接口服务的转发过程中,添加响应头允许前端域名跨域。
location / {
alias html;
index index.html index.htm;
add_header Access-Control-Allow-Origin "*"; #这里的*应该是前端域名 http://A
add_header Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT";
add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header Access-Control-Expose-Headers 'Content-Length,Content-Range';
proxy_pass http://192.168.1.6;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 解决办法3,:直接在接口程序那边加上允许指定前端页面跨域的响应头,这种办法不是很好,因为开发者不一定能确定后期部署时候前面页面允许跨域的域名。允许所有跨域只能出现在测试环境,或者说就不应该出现。
后端允许跨域参考