1.下载nginx 下载地址:http://nginx.org/en/download.html 2.解压压缩包 3.找到conf/nginx.conf文件,并修改
#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 9001; #修改端口号,只要不被占用即可 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 新增服务端口 server { listen 80; # 端口号 server_name 127.0.0.1; # IP(服务器) location / { root D:/wd-goods; #访问文件根目录 autoindex on; #是否浏览文件下的列表 add_header Access-Control-Allow-Origin *; #是否允许跨域 add_header Cache-Control "no-cache,must-revalidate"; # 是否缓存 } } }4.启动nginx 5.测试 浏览器输入:http://Ip+port 可以看到访问文件根目录,表示成功 完成!
1.安装依赖库 yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel 2.下载nginx 下载地址:http://nginx.org/download/ 下载完成后,压缩包上传至Linux服务器 3.解压压缩包 解压命令:tar -zxvf nginx-xxxx.tar.gz 4.安装nginx cd nginx-xxxx 进入到解压后的nginx文件夹 运行以下命令: 配置nginx启动文件和配置文件地址,并安装 ① ./configure --sbin-path=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf ② make ③ make install 5.修改配置文件 cd /usr/local/nginx vi nginx.conf
user root; #若在访问的时候报403错误,修改这一行,把原有的“#user nobody”改为“user root”, 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 8090; #修改端口号,只要不被占用即可 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #-----------------start-------------------------------- #添加以下代码 location /static/ { #“/static/”表示静态资源访问路径,即http://Ip:8090/static/resource.png root /root/wdService/; # 静态资源访问路径映射地址,即http://Ip:8090/static指向/root/wdService/static } #-----------------end-------------------------------- #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; } } }6.启动nginx ① cd /usr/local/nginx 找到nginx 启动文件 ② ./nginx -c /usr/local/nginx/nginx.conf 指定修改过的nginx配置文件 ③ ps -ef | grep nginx 查看nginx启动情况 7.访问测试 浏览器输入:http://Ip:port。出现nginx欢迎界面说明nginx启动成功 浏览器输入:http://Ip:port/static/resource.png 访问成功 完成!