账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Centos7 nginx配置问题
    34
    0

    —— 问题描述:

    在服务器中php-fpm启动的端口为:127.0.0.1:9000,
    php项目的存放路径为/data/iantoo/admin/;
    当我的nginx配置为如下的时候可以正常访问到php文件并被解析。

    location / {
        root   /data/iantoo/admin;
        index  index.html index.htm index.php;
    }
    
            
    location  ~ \.php$ {
        root           /data/iantoo/admin;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }  

    此时访问www.aaa.com可以正常访问,但是我需要通过www.aaa.com/admin这样的路径访问到项目,默认的www.aaa.com需要代理到别的端口。
    请问应该如何配置我的nginxlocation选项?

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 宇智波可达鸭 普通会员 1楼

      在CentOS 7上配置Nginx,你需要遵循以下步骤:

      1. 安装Nginx: 首先,需要在你的终端中安装Nginx。使用以下命令安装Nginx: sudo yum install nginx

      2. 配置Nginx: 使用文本编辑器打开/etc/nginx/nginx.conf文件。在这里,你可以修改Nginx的配置文件。以下是基本的Nginx配置文件示例: ```nginx server { listen 80; server_name example.com;

        location / { index index.html; try_files $uri $uri/ =404; }

        location ~ .php$ { fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; }

        location ~ /.ht { deny all; } } ```

      在上述配置中,我们设置了监听80端口,主机名为example.com。对于静态文件(例如,index.html),我们将它们保存在特定的目录中(在这里,我们使用了"/var/run/php/php7.4-fpm.sock")。

      1. 配置防火墙: 如果你想在Nginx服务器上配置防火墙,你需要使用firewalld软件。首先,你需要在终端中运行以下命令安装firewalld: sudo yum install firewalld

      然后,你需要在/etc/default/firewalld文件中添加以下行: firewalld服务 enable

      这将启动防火墙服务。

      1. 配置SSL: 如果你的网站需要HTTPS,你需要启用SSL。你可以使用以下命令启用SSL: sudo systemctl enable nginx

      2. 配置负载均衡: 如果你需要使用Nginx进行负载均衡,你可以使用Nginx的location指令。例如,如果你想将请求转发到一个后端服务器(例如,127.0.0.1:8000),你可以使用以下配置: ```nginx server { listen 8000; server_name example.com;

        location / { proxy_pass http://127.0.0.1:8000; } } ```

      在上述配置中,我们将请求转发到127.0.0.1:8000。

      1. 配置错误处理: 你可能需要添加错误处理代码来处理可能的服务器错误。你可以使用以下命令添加错误处理代码: ``` server { listen 80; server_name example.com;

        location / { index index.html; try_files $uri $uri/ =404; error_page 500 /500.html; }

        location ~ .php$ { fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; }

        location ~ /.ht { deny all; } } ```

      在上述配置中,我们添加了一个500错误页面,当服务器出现错误时,将用户重定向到500错误页面。

      1. 配置重定向: 你可能需要添加重定向规则来处理可能的错误。你可以使用以下命令添加重定向规则: ``` server { listen 80; server_name example.com;

        location / { index index.html; try_files $uri $uri/ =404; error_page 500 /500.html; redirect /error/404.html; }

        location ~ .php$ { fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; }

        location ~ /.ht { deny all; } } ```

      在上述配置中,我们添加了一个重定向规则,当服务器出现错误时,将用户重定向到错误页面。

      以上就是在CentOS 7上配置Nginx的基本步骤。如果你遇到任何问题,可以查阅Nginx的官方文档或在线资源。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部