云服务器Nginx配置
使用的是ubuntu云服务器,简单配置一下服务器
一、连接到服务器上
ssh @xx.xx.xxx.xxx //服务器外网IP地址
然后按照提示输入密码即可
二、使用Nginx:
sudo apt-get update //更新软件源
sudo apt-get install nginx //安装nginx
nginx -v //检查是否安装成功
登录阿里云,确认域名解析到了服务器IP上,并且已经备案成功,此时访问域名,就能看到:
这就说明nginx已经安装并基本部署好了
三、Nginx配置
配置文件
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
配置文件为:/etc/nginx/nginx.conf
和/etc/nginx/conf.d/default.conf
- 自定义nginx.conf配置
events {
worker_connections 1024; # 单个后台进程的最大并发数
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on; #开启高效传输模式
tcp_nopush on; #减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; #保持连接的时间,也叫超时时间
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on; #开启gzip压缩
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
# include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
- 自定义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/pc; #pc
# nginx适配pc和app设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/html/app; #app
}
index index.html; #默认访问文件
allow all; #允许访问的ip
# deny all; #拒绝访问的ip
}
# redirect error pages to the static page /404.html
error_page 404 /static/html/404/404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /static/html/404/500.html; #错误状态码的显示页面,配置后需要重启
# ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,
# 可以被规则^~ /static/ /aa匹配到(注意是空格)。
location ^~ /static/ {
root /usr/share/nginx/html;
allow all; #允许访问的ip
# deny all; #拒绝访问的ip
}
location = /50x.html {
root /usr/share/nginx/html/pc/; #pc
# nginx适配pc和app设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/html/app/; #app
}
}
# 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;
#}
}
修改配置
进入/etc/nginx
所在目录的conf.d
或sites-enabled
文件夹下编写配置文件
区别在于conf.d
下的文件必须以.d
结尾,而且服务器优先解析conf.d
文件夹下文件
还可以修改nginx.conf配置文件,如下:
修改http->server,简单的配置文件:
server {
listen 80;
server_name 域名;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 503 /50x.html;
location = /50x.html {
}
}
/etc/nginx/nginx.conf
中把运行用户改成root:
user root
修改完配置,重启一下nginxsudo nginx -s reload,就能正常运行了。
ps:如果还不行的话,清楚下浏览器缓存,重新登陆页面
四、上传代码
scp -r local_dir [email protected]:/remote_dir
五、Nginx相关命令:
- 启动nginx服务:
nginx
- 查看所有启动的nginx进程
ps aux | grep nginx
- 查看nginx配置
nginx -t
- 重启nginx服务
sudo nginx -s reload
- 查看端口占用情况
netstat -tlnp