【CentOS 7】nginx配置web服务器

1,安装过程

[[email protected]_1_14_centos ~]# cd /data/
[[email protected]_1_14_centos data]# wget  http://nginx.org/download/nginx-1.15.7.tar.gz
[[email protected]_1_14_centos data]# tar -xvf nginx-1.15.7.tar.gz
[[email protected]_1_14_centos data]# mkdir //usr/local/nginx -p
[[email protected]_1_14_centos data]# ll
total 1008
drwxr-xr-x 9 1001 1001    4096 Dec 17 15:16 nginx-1.15.7
-rw-r--r-- 1 root root 1026732 Nov 27 22:51 nginx-1.15.7.tar.gz
[[email protected]_1_14_centos data]# cd nginx-1.15.7/
[[email protected]_1_14_centos nginx-1.15.7]# ./configure --prefix=/usr/local/nginx
[[email protected]_1_14_centos nginx-1.15.7]#make&&make install

2,假设服务器外网IP为129.129.129.129,需要通过web访问的index文件是  /usr/index.html,index.html文件内容是:

<!DOCTYPE html>
<html>
        <head>
                <meta charset="UTF-8">
                <title></title>
        </head>
        <body>
                <h1>标题1</h1>
                <h2>标题2</h2>
                <h3>标题3</h3>
                <h4>标题4</h4>
                <h5>标题5</h5>
                <h6>标题6</h6>
                <h7>标题7</h7>
        </body>
</html>

同时,需要访问某个图片文件/usr/1.png

3,此时我们需要配置nginx

[[email protected]_1_14_centos /]# cd /usr/local/nginx/conf/
[[email protected]_1_14_centos conf]#
[[email protected]_1_14_centos conf]#
[r[email protected]_1_14_centos conf]#
[[email protected]_1_14_centos conf]# vim 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       80;             #这里需要我们设置web访问的端口
        server_name  129.xxx.xxx.xxx;       #这里设置web访问的IP,最终在浏览器访问    129.xxx.xxx.xxx:80/index.html    

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                                  #这里很重要,原因参考下一行
            root   /usr;                              #这里更重要,root是指直接访问 IP:port 时,获取文件的根目录,如果上一行设置为 / ,则直接访问IP:port会去拉取 /usr下面的index.html
            index  index.html index.htm;              #接上一行,如果location后面设置了目录  /abc ,则访问IP:port时,会拉取 /usr/abc/index.html.
        }

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

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

}

4,启动nginx服务,使用上面的配置文件

[[email protected]_1_14_centos sbin]# pwd
/usr/local/nginx/sbin
[[email protected]_1_14_centos sbin]# ./nginx -t #检查配置文件是否正确无误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected]_1_14_centos sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf  #使用上一步配置的nginx.conf启动nginx服务
[[email protected]_1_14_centos sbin]# ps -aux | grep nginx
root      6341  0.0  0.0  20552   620 ?        Ss   16:04   0:00 nginx: master process ./nginx -c /usr/local/nginx/conf/nginx.conf
nobody    6342  0.0  0.0  23088  1396 ?        S    16:04   0:00 nginx: worker process
root      6360  0.0  0.0 112708   980 pts/1    S+   16:05   0:00 grep --color=auto nginx

5,使用浏览器打开对应IP:port

如上图,直接访问IP,会使用默认端口80并拉取index.html.

如果我们需要访问web服务器上的1.png图片,只需要访问url    129.xxx.xxx.xxx:80/1.png,若端口号设置为80时,在浏览器中访问也可以不带端口号。

【Finished】

附:

nginx常用命令

./nginx -s reload          重启nginx

./nginx -s stop             停止nginx

原文地址:https://www.cnblogs.com/BH8ANK/p/10132183.html

时间: 2024-07-29 22:51:18

【CentOS 7】nginx配置web服务器的相关文章

centos从头学习配置web服务器环境

为了学习linux下配置web服务器环境,于是安装了vmware,准备在虚拟机里面学习web服务器的搭建! 首先是在虚拟机里安装centos,我选择的是32位的centos6.6版本,因为新版本7据说更改了好多命令,况且是新版本,于是没有采用! 至于选择32位的是因为我回头的vps的环境最大也就是2G(没有毛爷爷啊...) 所以就选择了32位的! 虚拟机安装centos基本上没啥说的,一直next就行了!(PS:vmware11 真心不错...) ------------------------

nginx配置web服务器

一:设置虚拟服务器 1.设置 http { server { listen 127.0.0.1:8080; server_name example.org www.example.org; } } 2.解释 如果有多个服务器与请求的IP地址和端口相匹配,则NGINX将根据服务器块中的server_name指令测试请求的主机头域.  server_name的参数可以是完整(精确)名称,通配符或正则表达式. 通配符是一个字符串,其开头,结尾或两者都包含星号(*); 星号匹配任何字符序列. NGINX

linux使用nginx配置web服务器

环境: CenterOS 7 1.安装nginx之前先安装nginx所需的依赖包 yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel 2.使用wget下载nginx压缩文件,并且解压安装,操作步骤如下: 点击查看官网nginx版本,此处我使用的是1.16.1版本 [[email protected]_1_14_centos ~]# cd /data/ [[email protected]_1_14_centos

linux学习笔记——搭建基于nginx的web服务器、多核配置、nginx配置参数

############ 认识nginx #############Nginx:(发音同 engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用.  其优点是轻量级(占有内存少),高并发(并发能力强),事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用ngi

nginx配置图片服务器

这几天研究了一下nginx配置图片服务器的相关内容,个人的一些收获与大家分享一下: Nginx是目前非常流行的web服务器,它起源于俄罗斯.它具有处理速度快,并发量大,占用资源极低等优点,尤其对于静态资源的处理更佳,有测试证明是apache的30倍.现在已经广泛的应用于多家门户网站.中大型网站中,作为反向代理.图片缓存服务器等.本例是结合张宴的blog(http://blog.s135.com/nginx_cache/),做的变动.主站是IIS7.5(192.168.36.70),用的是.net

nginx作为web服务器的应用

实验前提:1.本次实验我使用的系统平台为RHEL5.82.由于在测试时是基于域名来访问的,因此,需要修改系统上的hosts文件,如:www.xsl.com     192.168.0.104www.a.org       192.168.0.104 nginx作为web服务器的应用1.创建非特权用户由于nginx在运行时是以非特权用户的方式进行的,因此,在编译安装前需要创建一个非特权用户[[email protected] ~]#groupadd -r -g 200 nginx[[email p

nginx高性能web服务器详解(1)--安装nginx

1. 下载 本次使用nginx-0.1.2.3 版本,下载地址 http://nginx.org/en/download.html  新发布版本 http://nginx.org/download  历史版本 2.上传到linux服务器 sz -bey nginx-0.1.2.3.tar.gz 3.解压 3.1 建立目录  mkdir nginx_123 3.2 解压 tar -zxvf nginx-0.1.2.3.tar.gz ./nginx_123/ 4.配置编译环境 nginx源代码的编译

构建基于Nginx的web服务器

构建基于Nginx的web服务器 一.简介 Nginx("engine x") 是一个高性能的HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器. Nginx 是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站 点开发的,它已经在该站点运行超过四年多了.Igor 将源代码以类BSD许可证的形式发布.自Nginx 发布四年来,Nginx 已经因为它的占有内存少.并发能力强.稳定性.丰富的功能集.示例配置文件和低系统资源的消耗而闻名了.目前国内各大

nginx 配置web 虚拟目录 并且codeIgniter,thinkphp 重新url 地址

nginx 配置虚拟目录并且url 重写 server { #侦听80端口 listen 8090; #定义使用www.xx.com访问 server_name 127.0.0.1; #设定本虚拟主机的访问日志 access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /home/lxy/www/cs/; #定义服务器的默认网站根目录位置 #默认请求 location / { index inde