nginx 安装配置

直接采用压缩包下载安装
第一步,安装依赖

yum update yum
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel pcre-devel openssl openssl-devel

第二步,找个位置下载
例如我 的linux 路径 /home/home1/opt/nginx

wget http://nginx.org/download/nginx-1.13.7.tar.gz

然后解压

tar -xvf nginx-1.13.7.tar.gz

进入nginx目录

cd  /home/home1/opt/nginx

执行命令

# 生成配置
 ./configure
# 编译
# 安装
make && sudo make install

如果需要指定nginx的安装目录 例如: /opt/nginx下,则上述的   ./configure 需要添加如下参数

   ./configure --prefix=/opt/nginx \
               --sbin-path=/opt/nginx/sbin/nginx \
               --conf-path=/opt/nginx/conf/nginx.conf \
               --error-log-path=/opt/nginx/logs/error.log \
               --http-log-path=/opt/nginx/logs/access.log \
               --with-http_auth_request_module \
               --pid-path=/opt/nginx/run/nginx.pid 

然后执行如下命令启动

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

配置nginx.conf

贴出我自己的一个配置文件


#user  nobody;
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       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /opt/app/nginx/html/resouce;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; # 刷新页面404 处理
            # 添加 CORS 配置
	       add_header 'Access-Control-Allow-Origin' '*';
	       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
	       add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
		  add_header 'Access-Control-Allow-Credentials' 'true';

        }
        # 多个页面代理的时候,不能再用root 需要用alias
       location /web {
            alias   /opt/app/nginx/html/web;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; # 刷新页面404 处理

        }

        #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;
        }

        location  /api/ {
            proxy_pass http://127.0.0.1:9999/; #api请求地址的实际地址
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_redirect off;
         }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    
	
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /opt/app/nginx/html/xuni;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; # 刷新页面404 处理
            # 添加 CORS 配置
	       add_header 'Access-Control-Allow-Origin' '*';
	       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
	       add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
	       add_header 'Access-Control-Allow-Credentials' 'true';
        }

        #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;
        }

        location  /api/ {
            proxy_pass http://127.0.0.1:9999/; #api请求地址的实际地址
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_redirect off;
         }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

配置好后,重启nginx

#重启
/usr/local/nginx/sbin/nginx -s reload

#停止
/usr/local/nginx/sbin/nginx -s stop

我配置文件中的端口为8088,所以我的访问如下

这样,nginx就配置好了

docker安装nginx: 参考文字docker安装nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寂寞旅行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值