nginx常用配置

401 未授权:登录失败
403  禁止:禁止执行访问 
404 找不到 
500 服务器的内部错误 
502 网关出错

503 服务不可用 

 

yum安装nginx:

#添加yum源

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

#安装

yum install -y nginx

#安装后默认目录

/etc/nginx   #配置文件目录

/usr/share/nginx/html  #默认index.html

#默认bin目录

/usr/sbin/nginx -t

默认配置文件: 

/etc/nginx/nginx.conf和/etc/nginx/conf.d/default.conf

启动: 

systemctl enable nginx.service

systemctl start nginx.service

指定配置文件启动:

nginx -c nginx.conf

 

例子1: 默认按装后可修改默认主页,

例子2: 默认安装后也和修改默认主页目录

vi  /etc/nginx/conf.d/default.conf

location / {
#root /usr/share/nginx/html;
root /data/nginx;     #不需要chmod修改 /data/nginx的权限,因为755权限已经允许nginx用户登录
index index.html index.htm;
}

#注意: aws上修改后会报403错误,是因为默认aws没关闭selinux,关闭即可.

例子3: 也可用软链的方式来修改主页目录

cd /usr/share/nginx/html

ln -s /data/nginx/index.html index.html

 

1/ 转发:

upstream opossuperset_backend{
server 10.52.2.243:6666;
keepalive 1000;
}

server{

location / {
proxy_pass http://opossuperset_backend;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-For $remote_addr;;

proxy_set_header X-Real-IP $http_x_real_ip;
}
}

 

2/ 直接转发内网

server {
listen 80;
server_name www.fengfeng.com;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
proxy_pass http://10.52.172.94:6666;
}
location = /test {
proxy_pass http://10.52.174.112:6666;
}
error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

 

3. 转到另一个域名

upstream backend {  
    server 127.0.0.1:8080;  
}  
upstream crmtest {  
    server crmtest.aty.sohuno.com;  
}  
server {  
        listen       80;  
        server_name  chuan.aty.sohuno.com;  
        proxy_set_header Host $http_host;  
        proxy_set_header x-forwarded-for  $remote_addr;  
        proxy_buffer_size         64k;  
        proxy_buffers             32 64k;  
        charset utf-8;  
  
        access_log  logs/host.access.log  main;  
        location = /50x.html {  
            root   html;  
        }  
    location / {  
        proxy_pass backend ;  
    }  
          
    location = /customer/straightcustomer/download {  
        proxy_pass http://crmtest;  
        proxy_set_header Host $proxy_host;  
    }  
}  

当匹配到/customer/straightcustomer/download时,使用crmtest处理,到upstream就匹配到crmtest.aty.sohuno.com,这里直接转换成IP进行转发了。
假如crmtest.aty.sohuno.com是在另一台nginx下配置的,ip为10.22.10.116,则$proxy_host则对应为10.22.10.116。此时相当于设置了Host为10.22.10.116。

 

4、 配置跨域访问, 在http标签中添加:

add_header 'Access-Control-Allow-Origin' '*';

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';

 

4/ 自动索引下载目录

在入口机(ali-bigdata-gateway-guard-1-10.52.5.222)上把需要下载的文件放/usr/share/nginx/html/download目录下
访问http://8.208.9.102/download/
找到您的文件,点击鼠标右键,选择链接另存为,把文件下载到本地
注意:保密文件,记得下载完去服务器上删除


https://www.cnblogs.com/Dev0ps/p/11210639.html

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
#root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /download {
#root /usr/share/nginx/html/download;
alias /usr/share/nginx/html/download;
charset utf-8;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}

补充:用tomcat实现

https://www.cnblogs.com/ruanjianlaowang/p/11182720.html

 

#nginx问题:

1、 yum装上后 ,把G盘opay下的转发conf拷贝过去转发proxy不生效 

要把 nginx.conf默认的注销掉 ,不然会直接访问默认的centos页面

    server {
       # listen       80 default_server;
        #listen       [::]:80 default_server;
       # server_name  _;
       # root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

conf.d:  bigdata-admin.conf

    server {
        listen       80;
        server_name  xxx.opaydev.com;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://10.52.5.213:80;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

  

 

posted on 2019-11-07 14:45  锋锋2019  阅读(837)  评论(0)    收藏  举报

导航