Centos安装Nginx,并配置代理Tomcat和404错误页面

392 阅读1分钟

1.下载Nginx

通过rpm方式来安装nginx,先下载rpm包:nginx.org/packages/rh….

或者可以直接wget nginx.org/packages/rh…

安装:rpm -ivh nginx-1.8.1-1.el7.ngx.x86_64.rpm

2.配置代理Tomcat

配置/etc/nginx/nginx.conf

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

	#添加服务器监听80端口
    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        access_log  logs/host.access.log  main;
		#tomcat 代理转发
        location / {
            proxy_pass http://localhost:8080;
            #开启代理的服务器的错误处理
            proxy_intercept_errors on;

        }
		
		#404跳转
        error_page  404              /404.html;
		location = /404.html {
			#存放页面的路径 下同
            root   /usr/share/nginx/html/;
        }
		
        #50x跳转
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html/;
        }
		#指定图片路径 当在页面发现图片资源502的时候是因为没有配置图片的路径
		location ~ .*\.(gif|jpg|jpeg|png)$ {
			root /usr/share/nginx/html/;
		}
    }
	
}

3.检验一下

nginx -c /etc/nginx/nginx.conf

这就算成功了,再把服务停掉看看500页面 (从这里开始我把Apache-Tomcat的ROOT目录改成了test,即访问路径变成了192.168.1.131/test)

再看下404