Ubuntu系统nginx的安装与配置
Nginx 是一个很强大的高性能和反向代理服务
1.安装nginx
sudo apt-get install nginx
过程会让选一个Y同意占用内存。
2.如果出现无法定位nginx包,进行如下操作:
sudo apt-get update
3.更新完成之后,安装nginx
sudo apt-get install nginx
4.更改配置文件,以当前最新的1.13.5版本为例配置示例如下:
可以先将原本的配置文件备份
cp /etc/conf/nginx.conf /etc/conf/nginx.conf.cp
备份好配置文件nginx后,用vi编辑器打开etc/conf目录下的nginx.conf,对配置文件进行修改,模板如下(建议直接拷贝)
sudo vi /etc/conf/nginx.conf
#运行用户
#user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及连接数上限
events {
#单个后台worker process进程的最大并发链接数
worker_connections 1024;
}
http {
#设定mime类型,类型由mime.type文件定义
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;
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
#gzip on;
#设定虚拟主机配置,同一个nginx可配置多个server
server {
#侦听80端口
listen 80;
server_name helloui.net;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;
#默认请求
location / {
proxy_pass http://127.0.0.1:8091/;
root html;
index index.html index.htm;
proxy_set_header Host $host:$server_port;
}
#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;
}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
# 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;
# }
#}
}
上述反向代理表示,访问端口
80
(默认的浏览器网站)时,先到达反向代理服务器nginx,nginx再把请求转发服务主机为127.0.0.1,端口为8090(根据自己得需要自行更改)的后台服务。
5.Ubuntu安装之后的文件结构大致为:
- 1)所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下
- 2)程序文件在/usr/sbin/nginx
- 3)日志放在了/var/log/nginx中
- 4)并已经在/etc/init.d/下创建了启动脚本nginx
- 5)默认的虚拟主机的目录设置在了/var/www/nginx-default (有的版本默认的虚拟主机的目录设置在了/var/www, 请参考/etc/nginx/sites-available里的配置)
6.nginx的使用
- 检查配置文件nginx.conf的正确性命令:
nginx -t
- 启动Nginx
nginx
- Nginx其他命令
sudo nginx -s reload #重新加载配置文件
sudo nginx -s reopen #重新启动Nginx
sudo nginx -s stop #停止Nginx
7.访问
启动nginx后,此时用户即可打开浏览器打开的页面访问(http://127.0.0.1)最终访问的便是后台web服务主页: http://127.0.0.1:8090/index.html