Nginx 入门级配置
2019独角兽企业重金招聘Python工程师标准>>>
Ubuntu下安装Nginx
sudo apt-get install nigx
启动
sudo nginx
在浏览器中输入
http://localhost:80/
出现 Welcome to nginx! 安装成功
重启
sudo service nginx restart
停止
sudo service nginx stop
静态目录访问
查找nginx.conf
sudo find / -name nginx.conf
结果:/etc/nginx/nginx.conf
修改默认静态文件目录,查看/etc/nginx/nginx.conf; 会发现里面有一条
include /etc/nginx/sites-enabled/*;
, 那去site-enabled
目录看看吧,查看/etc/nginx/site-enable/default
文件;找到root /usr/share/nginx/www;
, 去这个目录看看吧,发现有个index.html;那么看看内容发现是’Welcome to nginx!‘
所以修改静态文件夹就是修改root 后面那一串自己的数字就可以了
如果像浏览自己的文件目录
location /a/ {autoindex on;
}
负载均衡配置(后端使用tornado)
先写一个tornado的hello.py
import tornado.ioloopfrom tornado.options import define, optionsimport tornado.webdefine("port", default=8888, help="run on the given port", type=int)tornado.options.parse_command_line()class MainHandler(tornado.web.RequestHandler):def get(self):s = "Hello, world "+str(options.port)self.write(s)application = tornado.web.Application([(r"/test/?", MainHandler),(r"/", MainHandler),])if __name__ == "__main__":application.listen(options.port)tornado.ioloop.IOLoop.instance().start()
启动web: python hello.py -port=9000
浏览器输入:http://localhost:9000 就会看到结果了
使用nginx负载均衡,修改nginx.conf参考tornado官网
user nginx;worker_processes 1;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events {worker_connections 1024;use epoll;}http {# Enumerate all the Tornado servers hereupstream frontends {server 127.0.0.1:8000;server 127.0.0.1:8001;server 127.0.0.1:8002;server 127.0.0.1:8003;}include /etc/nginx/mime.types;default_type application/octet-stream;access_log /var/log/nginx/access.log;keepalive_timeout 65;proxy_read_timeout 200;sendfile on;tcp_nopush on;tcp_nodelay on;gzip on;gzip_min_length 1000;gzip_proxied any;gzip_types text/plain text/html text/css text/xmlapplication/x-javascript application/xmlapplication/atom+xml text/javascript;# Only retry if there was a communication error, not a timeout# on the Tornado server (to avoid propagating "queries of death"# to all frontends)proxy_next_upstream error;server {listen 80;# Allow file uploadsclient_max_body_size 50M;location ^~ /static/ {root /var/www;if ($query_string) {expires max;}}location = /favicon.ico {rewrite (.*) /static/favicon.ico;}location = /robots.txt {rewrite (.*) /static/robots.txt;}location / {proxy_pass_header Server;proxy_set_header Host $http_host;proxy_redirect false;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Scheme $scheme;proxy_pass http://frontends;}}}
重启nginx:
sudo service nginx restart
启动多个tornado(多个终端启动,或者是后台启动):
python hello.py -port=8000
python hello.py -port =8001
python hello.py -port=8002
python hello.py -port =8003
浏览器输入: http://localhost, 发现没反应, 查看nginx错误日志:/var/log/nginx/error.log出现问题:getpwnam("nginx") failed 解决问题:
sudo adduser --system --no-create-home --disabled-password --group nginx
重启ngixn, 再试浏览器;Ok,刷新浏览器,会发现每次返回的端口都是不一样;说明后面的都访问到了
###总结
以上的配置都是nginx最常用的入门级配置;有了这些基础就可以去找官网找些适用于自己的配置; 其他配置: 参考博客http://blog.s135.com/post/306
转载于:https://my.oschina.net/jiemachina/blog/185484
Nginx 入门级配置相关推荐
- nginx技术(2)nginx的配置详解
nginx的配置 1,启动nginx 1 2 3 4 5 6 7 [root@centos6 nginx-1.2.9]# /usr/sbin/nginx -c /etc/nginx/nginx.con ...
- 实现nginx上配置免费证书Let's Encrypt
Let's Encrypt 的免费证书有效期为三个月,不过可以免费续期,写一个脚本定期更新即可. 准备一台nginx 服务器 ,将以下三个附件上传到你的nginx服务器. 1.下载脚本文件,wget ...
- 推荐一款 Nginx 可视化配置神器
以下文章来源方志朋的博客,回复"666"获面试宝典 来源:逛逛GitHub Nginx 是前后端开发工程师必须掌握的神器.该神器有很多使用场景,比如反向代理.负载均衡.动静分离.跨 ...
- 九爷带你了解 nginx 日志配置指令详解
nginx日志配置指令详解 日志对于统计排错来说非常有利的. 本文总结了nginx日志相关的配置如 access_log.log_format.open_log_file_cache.log_not_ ...
- php和nginx安装脚本,Nginx + PHP 配置和启动脚本
Nginx + PHP 配置和启动脚本,很实用 # nginx.conf server { listen 8080 ; server_name localhost; location / { root ...
- php修改后nginx返回不生效,nginx修改配置后不生效的问题
nginx增加了新的server name配置,发现nginx -s reload之后总是不生效. http和https均可以打开页面,但是页面是别的server页面,使用的证书也是别的server的 ...
- nginx lua 配置cc 防攻击-使用lua 配置黑白名单
nginx lua 配置cc 防攻击-使用lua 配置黑白名单 cc 防攻击和ip 禁止期限 lua_shared_dict _dict 1m; lua_shared_dict _blacklist ...
- nginx基本配置与参数说明
user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; #er ...
- 用Nginx如何配置运行无扩展名PHP文件或非.PHP扩展名文件
用Nginx如何配置运行无扩展名PHP文件或非.PHP扩展名文件 使用Apache + PHP 很容易做到运行无扩展名的PHP文件. 在Nginx中能做到吗?是可以的. 只需将nginx.conf文件 ...
最新文章
- [ASP.NET 控件实作 Day8] 控件常用 Attribute 介绍(1)
- Coursera吴恩达《序列模型》课程笔记(2)-- NLP Word Embeddings
- 6-day6-函数-1
- 图解TCPIP-NAT
- 在远程桌面服务中配置RD网关直接访问内网
- HDU4712 Hamming Distance (随机化)
- 【Hive】条件函数
- 2.冒泡排序----还是不懂,先记录下来
- java 解析xml saxreader_Java中使用DOM和SAX解析XML文件的方法示例
- Javascript---js的编码及解码
- 欧派caxa设计软件_CAXA软件三维设计的基本使用方法
- 关于U盘物理读写锁失效解决(只读状态):工具解锁或U盘量产
- springboot项目+多个启动类部署到linux服务器上
- 计算机维修与数据恢复论文,备份恢复论文,关于计算机数据备份数据恢复技术相关参考文献资料-免费论文范文...
- 快乐去学习「快乐机器学习」
- 使用SLT系统抽数到hana系统
- RuntimeError: stack expects each tensor to be equal size
- 自定义ImageView实现播放帧动画
- 音速索尼克 怪人_优势演员评论家方法简介:让我们玩刺猬索尼克!
- 对象赋值时this指向问题 obj1.say = obj2.say;obj1.say()