环境安装
[root@yh ~]yum install gcc-c++ |
[root@yh ~]yum install pcre pcre-devel |
[root@yh ~]yum install openssl openssl-devel |
下载 nginx
[root@yh src]wget http://nginx.org/download/nginx-1.12.2.tar.gz |
解压:
[root@yh src] tar -zxvf nginx-1.12.2.tar.gz |
进入加压后的目录:
安装:
[root@yh nginx-1.12.2]# ./configure |
执行 make 命令
[root@yh nginx-1.12.2]# mack |
[root@yh nginx-1.12.2]# make install |
查看nginx的安装位置
查看nginx的安装后的根目录
启动nginx
查看ngin的线程
[root@yh sbin]# ps aux|grep nginx
直接访问服务器的ip(nginx默认80端口)
配置:
打开配置文件:
[root@yh conf]# vim /usr/local/nginx/conf/nginx.conf |
默认配置文件 第一行添加: user ftpuser; 作为nginx访问该文件夹的账户
如果不配置用户可能会出现权限相关错误,比如这里没有这个配置,
当通过nginx访问系统文件时会出现下面错误
最下面的注释里找到HTTPS server 在它上面添加 文件引用的通配符配置:
include vhost/*.conf; |
新建自定义配置文件所在的文件夹vhost
[root@yh conf]# mkdir /usr/local/nginx/conf/vhost |
在要访问nginx的客户机(win10)上配置hosts文件,
C:\Windows\System32\drivers\etc\hosts 添加下面的内容
其中 的ip为nginx服务器的(centos6.8)IP
在vhost文件夹中添加文件:www.yh.com.conf 配置网页类型的域名
server {
default_type 'text/html';
charset utf-8;
listen 80;
autoindex on;
server_name www.yh.com;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#root /devsoft/apache-tomcat-7.0 .73/webapps/mmall;
#error_page 404/404. html;
if ($query_string ~* ".*[\;'\<\>].*") {
return 404;
}
location / {
proxy_pass http://127.0.0.1:8080/;
add_header Access-Control-Allow-Origin '*';
}
}
其中http://127.0.0.1:8080/ 为已经运行的tomcat
重启nginx
[root@yh conf]# pwd
/usr/local/nginx/conf
[root@yh conf]# ../sbin/nginx -s reload
访问结果:
在vhost文件夹中添加文件:image.yh.com.conf 配置图片类型的二级域名
server {
listen 80;
autoindex off;
server_name image.yh.com;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#root /devsoft/apache-tomcat-7.0 .73/webapps/mmall;
#error_page 404/404. html;
if ($query_string ~* ".*[\;'\<\>].*") {
return 404;
}
location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
deny all;
}
location / {
root /home/ftpuser/img/;
add_header Access-Control-Allow-Origin *;
}
}
其中 /home/ftpuser/img/ 下面已经放好了照片
继续重启nginx:
[root@yh conf]# pwd
/usr/local/nginx/conf
[root@yh conf]# ../sbin/nginx -s reload
访问结果:
一张与火箭奥沙利文的合影奉上