Nginx 配置
常用命令
- 检查文件是否正确:
nginx -t -c /etc/nginx/nginx.conf
直接检查 nginx.conf 会检查内部所有 include 的自定义配置文件
代理实际路径
Ant Design Pro 配置
server {
listen 80;
server_name demo_erp.liruwei.cn;
return 301 https://demo_erp.liruwei.cn$request_uri;
}
server {
listen 443 ssl http2;
server_name demo_erp.liruwei.cn;
include /root/https/liruwei.cn.ssl.conf;
index index.html;
root /home/www/demo_erp.liruwei.cn;
location / {
# 用于配合 browserHistory使用
try_files $uri $uri/ /index.html;
# 如果有资源,建议使用 https + http2,配合按需加载可以获得更好的体验
# rewrite ^/(.*)$ https://preview.pro.ant.design/$1 permanent;
}
location /api {
proxy_pass http://0.0.0.0:1337;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 数据压缩
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
}
共享盘
- 创建共享目录
mkdir -m 777 /home/downloads
- 配置 Nginx 文件
- 打开地址
192.168.0.6:1122/downloads
# 配置文件
server {
listen 1122;
# url 为 192.168.0.6:1122/downloads
# 实际上访问的是 /home/downloads
location /downloads {
root /home;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
}
Docker 运行
Nginx
比较特殊,因为它还需要以下3个特殊的配置文件: nginx.conf
,index.html
,default.conf
创建目录结构
创建目录结构,并把3个文件内容复制进去
/Users/liruwei/nginx/ # 根目录
|-- nginx.conf # 主配置文件
|-- html/
-- index.html # nginx 默认页面 index.html
|-- conf.d/
-- default.conf # 默认的子配置文件
|-- log/ # 日志文件
# 懒人命令创建命令
$ touch nginx.conf & \
mkdir html & \
sleep 1; touch html/index.html & \
mkdir conf.d & \
sleep 1; touch conf.d/default.conf & \
mkdir log
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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 {
root /usr/share/nginx/html;
}
# 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;
#}
}
index.html
Hello!
运行
$ docker run \
--name my_nginx \
-d -p 81:80 \
-v $(pwd)/html:/usr/share/nginx/html \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf \
-v $(pwd)/conf.d:/etc/nginx/conf.d \
-v $(pwd)/log:/var/log/nginx \
nginx
-
/usr/share/nginx/html
: nginx 默认的网页文件存放目录,如果需要部署前端的代码,建议提前规划挂载其他目录;或者在该目录下建立目录,并在配置文件中指定即可(为了安全,需要删除默认的 default.conf,指定网页被访问的域名) -
/etc/nginx/nginx.conf
:nginx 主配置文件。 -
/etc/nginx/conf.d
: nginx 子配置文件,一般域名配置,端口映射都放在该目录下,当然也可以放在其他目录;如果放置其他目录需要提前规划好,并设置相应的目录挂载。 -
/var/log/nginx
:nginx 的日志文件
密码访问
安装工具
# ubuntu
$ apt-get install apache2-utils
# centos
$ yum install httpd
创建账号密码
$ htpasswd -c /etc/nginx/my_user_password/yourfile user
其中 /etc/nginx/my_user_password/
是绝对路径, yourfile
为保存文件,user
为新增账号。
其他参数
-c:创建一个加密文件 -n:不更新加密文件,只将加密后的用户名密码显示在屏幕上 -m:默认采用MD5算法对密码进行加密 -d:采用CRYPT算法对密码进行加密 -p:不对密码进行进行加密,即明文密码 -s:采用SHA算法对密码进行加密 -b:在命令行中一并输入用户名和密码而不是根据提示输入密码 -D:删除指定的用户
注意:路径不能在
/root
目录下,不然会出现访问500
配置 nginx
server {
listen 443 ssl http2;
server_name nginx.liruwei.cn;
include /root/https/liruwei.cn.ssl.conf;
# 提示文字
auth_basic "Enter user and password";
# 账号密码保存文件
auth_basic_user_file /etc/nginx/my_user_password/yourfile;
}
访问限制
为了防止暴力破解,我们可以用nginx的ngx_http_limit_req_module模块进行访问限制,比如说:
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
...
server {
...
location /search/ {
limit_req zone=one burst=5;
}
}
}
上述配置的意思为:一秒处理不超过1个请求,突发请求不超过5个,否则返回错误信息。
location 配置
参考
wss 转发
参考