安装nginx
前提:Linux服务,可以本地搭建虚机(建议),也可以是一些云厂商提供的云服务主机。
参见Nginx 安装配置
安装好之后,在物理机打开浏览器器访问Nginx虚机。地址栏是nginx服务所在虚机的IP地址,默认端口80,效果图如下:
有一种可能,在访问的时候请求遭到拒绝。应该是因为Linux虚机防火墙给挡住了。
关于防火墙的操作:
Centos防火墙设置与端口开放的方法
打开nginx的日志可以看到请求内容:
另外,关于Nginx的四个命令:
nginx -c </path/to/config> 指定配置文件启动。
nginx -t 不启动Nginx,检查配置文件语法正确性,以及尝试打开配置文件中引用的文件。
nginx -v 显示版本
nginx -V 显示Nginx的版本,编译器版本和参数
另外还有:
nginx -s reload 重新加载配置文件
如果是在阿里云上安装,并且想要访问到。那么需要配置安全规则
然后重启,一定要重启实例。
默认配置文件解释 nginx.conf
#user nobody; #linux用户组和用户 格式如:user www www 涉及到权限问题可以使用 比如文件服务器 只有拥有权限的用户才可以下载指定路径文件
worker_processes 1; #设置值和CPU核心数一致,可以理解为器几个工作进程。
#worker_processes 2;
#worker_cpu_affinity 01 10; 01代表启用第一个cpu内核,10代表启动第二个cpu内核
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #日志位置和日志级别
#pid logs/nginx.pid;
#worker_rlimit_nofile 65535; #可以打开的最大文件描述符的值
events {
#包含nginx中所有处理连接的设置
worker_connections 1024; #工作进程最大连接数量 理论上每台nginx服务器的最大连接数为worker_processes*worker_connections
}
http {
include mime.types; #支持返回资源类型,例如image/jpeg
default_type application/octet-stream; #返回资源类型 这种类型会让浏览器播放文件而不是下载
#log_format main escape=json 可以防止汉字和标点符号被转义为16进制表示
#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; #虚机服务器的识别路径。不同域名会通过请求头中的HOST字段,匹配到特定的server块,转发到对应的应用服务器中去
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~ img{
#add_header Content-Disposition "attachment;"; #添加头部信息告诉浏览器是下载不是打开。
}
#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;
#}
}
#负载均衡demo
#upstream demoProxy{
# server 192.168.1.120:7851 weight=2 max_fails=3 fail_timeout=100s;
# server 192.168.1.121:7851 weight=2 max_fails=3 fail_timeout=100s;
#
#}
#
#server {
# listen 80;
# server_name demo.com;
# root /home/work/app/dist;
# index index.html;
# location /sys {
# proxy_pass http://demoProxy;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_next_upstream http_502 http_504 error timeout invalid_header;
# }
# }
#
#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;
# }
#}
}