nginx 配置示例
nginx操作
重启
./nginx -s reload
nginx返回文本
location ~ ^/get_text { default_type text/html; return 200 'This is text!'; }
nginx返回json
location ~ ^/get_json { default_type application/json; return 200 '{"status":"success","result":"nginx json"}'; }
nginx反向代理
location / { proxy_set_header Host $host; #保留代理之前的host proxy_set_header X-Real-IP $remote_addr; #保留代理之前的真实客户端ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr; #在多级代理的情况下,记录每次代理之前的客户端真实ip # limit_req zone=myRateLimit burst=20 nodelay; proxy_pass http://localhost:9081/; }
nginx配置ssh
ssl on; ssl_certificate /etc/nginx/cert/shnchuang.cn_nginx/4588866_suang.cn.pem; ssl_certificate_key /etc/nginx/cert/shopng.cn_nginx/4588866_shopnchuang.cn.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置 ssl_prefer_server_ciphers on;
php忽略php后缀
location / { try_files $uri $uri/ $uri.php?$args; }
nginx配置ssl+zip
server{ listen 443; server_name jys.test.cn; #填写绑定证书的域名 ssl on; ssl_certificate /etc/nginx/cert/jys.3dmenchuang.cn_nginx/4825885_jys.test.cn.pem; ssl_certificate_key /etc/nginx/cert/jys.3dmenchuang.cn_nginx/4825885_jys.test.cn.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置 ssl_prefer_server_ciphers on; location / { proxy_set_header Host $host; #保留代理之前的host proxy_set_header X-Real-IP $remote_addr; #保留代理之前的真实客户端ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr; #在多级代理的情况下,记录每次代理之前的客户端真实ip # limit_req zone=myRateLimit burst=20 nodelay; gzip on; gzip_disable "msie6"; gzip_comp_level 2; gzip_min_length 1100; gzip_buffers 16 8k; gzip_proxied any; gzip_types text/plain text/css text/js text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml; proxy_pass http://localhost:6005; proxy_redirect default; #指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值 } }
nginx访问静态
server { listen 80; server_name dc.3d.cn; #填写绑定证书的域名 root /www//dist; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # limit_req zone=myRateLimit burst=20 nodelay; index index.html index.php index.htm index.nginx-debian.html; try_files $uri $uri/ /index.html; } }
nginx访问txt列表
server { listen 80; server_name bick.yiui.cn; #填写绑定证书的域名 root /www/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; location ~ \.(mp4|png|txt) { root /www/file; autoindex on; } charset utf-8,gbk; #访问文件列表 location /log { alias /var/log/nginx/; #Nginx日志目录 autoindex on; #打开目录浏览功能 autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes #显示出文件的大概大小,单位是kB或者MB或者GB autoindex_localtime on; #默认为off,显示的文件时间为GMT时间。 #改为on后,显示的文件时间为文件的服务器时间 add_header Cache-Control no-store; #让浏览器不保存临时文件 } #直接下载访问内容 location /log/download { alias /var/log/nginx/; #Nginx日志目录 autoindex on; #打开目录浏览功能 autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes #显示出文件的大概大小,单位是kB或者MB或者GB autoindex_localtime on; #默认为off,显示的文件时间为GMT时间。 #改为on后,显示的文件时间为文件的服务器时间 add_header Cache-Control no-store; #让浏览器不保存临时文件, add_header Content-Disposition attachment; } location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # limit_req zone=myRateLimit burst=20 nodelay; index index.html index.php index.htm index.nginx-debian.html; try_files $uri $uri/ /index.html; } }
权限限制(查看nginx日志)
1.
yum -y install httpd-tools #安装htpasswd工具 cd /etc/nginx/conf.d #切换目录 htpasswd -c ./auth dever #使用htpasswd命令在当前目录创建一个名为auth的文件,用户为dever 回车后需要输入两次密码 more ./auth #查看auth文件里的内容,有用户名和加密的字符串
2.nginx配置
server { listen 80; server_name nginx_logs.yiui.cn; #填写绑定证书的域名 charset utf-8,gbk; #访问文件列表 location / { alias /var/log/nginx/; auth_basic "Auth access test!input your passward!"; auth_basic_user_file /etc/nginx/conf.d/auth; #Nginx日志目录 autoindex on; #打开目录浏览功能 autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes #显示出文件的大概大小,单位是kB或者MB或者GB autoindex_localtime on; #默认为off,显示的文件时间为GMT时间。 #改为on后,显示的文件时间为文件的服务器时间 add_header Cache-Control no-store; #让浏览器不保存临时文件 } }
..

浙公网安备 33010602011771号