一. 下载并安装 Nginx
去 Nginx 官网下载 http://nginx.org/en/download.html
我这里选取 nginx/Windows-1.10.3 http://nginx.org/download/nginx-1.10.3.zip 版本, 下载后解压出来即可, 解压出来的路径不能含有中文
我解压后将其放置的路径如下
二, 开始运行
在当前目录下按住 shift + 鼠标右键, 选择 "在此处打开命令窗口", 然后输入 start nginx
此时, 就可以进入浏览器输入访问地址, http://127.0.0.1 / 或者 http://localhost / 即可访问
三, 配置文件讲解
核心配置文件就是 nginx.conf, 该文件位于 conf 目录下, 大部分情况下我们就是修改该文件的配置
该文件的原始配置如下:
- #user nobody;
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include 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 logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root 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 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;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443 ssl;
- # server_name localhost;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.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;
- # }
- #}
- }
其中 #代表注释
nginx 我们最主要的作用是拿来做反向代理和负载均衡, 这个我后面会着重讲解. 同时它还是一个 web 服务器, 与我们常用的 Apache,tomcat,IIS 一样, 也可以用来托管 web 服务.
本章先暂时介绍下该配置文件中的几个重要参数, 后面会对 nginx 部署 php 和 Python 项目再进行着重讲解, 至于 java 的项目通常是 tomcat+nginx 同时进行配置, nginx 用来做负载均衡和处理静态页.
1, 定义 Nginx 运行的用户和用户组
#user nobody;
2,nginx 进程数, 建议设置为等于 CPU 总核心数
worker_processes 1;
3, 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
4, 进程文件
#pid logs/nginx.pid;
5, 工作模式与连接数上限: worker_connections 是单个后台 worker process 进程的最大并发链接数, 并发总数是 worker_processes 和 worker_connections 的乘积, 即 max_clients = worker_processes * worker_connections
- events {
- worker_connections 1024;
- }
6,http 下的一些配置及其意义
- include mime.types; #文件扩展名与文件类型映射表
- default_type application/octet-stream; #默认文件类型
- sendfile on; #开启高效文件传输模式, sendfile 指令指定 nginx 是否调用 sendfile 函数来 输出文件, 对于普通应用设为 on, 如果用来进行下载等应用磁盘 IO 重负载应用, 可设置 为 off, 以平衡磁盘与网络 I/O 处理速度, 降低系统的负载. 注意: 如果图片显示不正常 把这个改成 off.
- autoindex on; #开启目录列表访问, 合适下载服务器, 默认关闭.
- tcp_nopush on; #防止网络阻塞
- tcp_nodelay on; #防止网络阻塞
- keepalive_timeout 120; #长连接超时时间, 单位是秒
- gzip on; #开启 gzip 压缩输出
7,server 虚拟主机的相关配置
我们平时配置各类服务器, 配置最多的就是这些地方了
比如:
- http {#虚拟主机1 server {
- listen 80;#监听端口,
- 基于IP配置的时候变更此处,
- 比如192.168.1.100 : 8080;
- server_name www.xdw.com;#主机域名,
- 实际项目发布的话,
- 填公网上的域名,
- 本地部署的话,
- 可以在C: \Windows\System32\drivers\etc\hosts文件中添加IP和域名的映射location / {#映射解析,
- / 代表根路径, 此处解析还有正则表达式的解析方式, 具体请参考 http:/ / tengine.taobao.org / nginx_docs / cn / docs / http / ngx_http_core_module.html#location root E: /xdw/0221;#工程所在路径index index.html index.htm;#首页 (默认页)
- }
- }#虚拟主机2,
- 可以同时配置多个虚拟主机server {
- listen 8080;
- server_name localhost;
- location / {
- root D: /xiangmu/txym_web;
- index index.html index.htm;
- }
- }
- }
看到这个虚拟主机的配置, 相信配置过 tomcat 或者 Apache 的人都很熟悉的感觉. 此篇就到此结束, 下面还会更新 linux 下的配置, php 和 python 项目的部署, 反向代理和负载均衡, 配合 tomcat 部署 java 项目.
来源: https://www.cnblogs.com/xiadewang/p/8710951.html