nginx yum 安装nginx

首先在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo:

vi /etc/yum.repos.d/nginx.repo  

写入以下内容并保存:

[nginx] 
name=nginx repo  
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/  
gpgcheck=0  
enabled=1  

安装 nginx

yum install nginx -y # 安装Nginx  
service nginx start # 启动Nginx  
chkconfig nginx on # 设置开机启动Nginx  

这样安装的nginx与编译安装的配置文件不太相同。 修改 /etc/nginx/conf.d/default.conf 配置文件或者在 /etc/nginx/conf.d 目录下创建一个配置文件xxx.conf

vi /etc/nginx/conf.d/xxx.conf  

配置后保存退出,重启nginx:

service nginx restart  

这样就大功告成啦~

nginx 非80端口转发问题

如果 nginx 的监听端口不是默认的 80 端口,改为其他端口如 81 端口。后端服务器中 request.getServerPort() 无法获得正确的端口,返回的仍然是 80;在 response.sendRedirect() 时,客户端可能无法获得正确的重定向url。正确的配置方法为在 $host 之后加上端口号,如 $host:$server_port 或者 $host:81

server {  
        listen       83;
        server_name  localhost;
        location / {
         proxy_pass  http://147.16.24.175:9500;
         proxy_set_header   Host             $host:83;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Via    "nginx";
        }
 }
文件转发

中庆纳博对接:用 nginx 代理题库中 other 部分试题文件,使其指向云题库的文件,无需再部署一份。且能保证本地题库上传的文件映射到本地磁盘目录。

location /vfs/other {  
    proxy_pass http://zj.4ye.cc/vfs/other;
    client_max_body_size  3000m;
}
location /zj/vfs/other {  
    proxy_pass http://zj.4ye.cc/vfs/other;
    client_max_body_size  3000m;
}
location /vfs/ {  
    alias /opt/jdlserver_prd/vfsroot/store/;
    expires 30d;
    break;
}            
location /zj/vfs/ {  
    alias /opt/jdlserver_prd/vfsroot/store/;
    expires 30d;
    break;
}

Q.E.D.


Think Big, Act Small